Loading Depth Study
Preparing your workspace
TypeScript can describe the inputs a function accepts and the value it returns.
Function types make APIs between parts of an application clearer. Parameters can have explicit types and the return type can also be defined. TypeScript can often infer the return type, but explicit return types are useful for important public functions and complex logic. Optional parameters use a question mark, while default values provide a value when an argument is not supplied.
A learning platform can use a function that accepts completed lessons and total lessons as numbers and returns a percentage number.
function calculateProgress(completed: number, total: number): number {
if (total === 0) return 0;
return Math.round((completed / total) * 100);
}
const progress = calculateProgress(8, 10);
console.log(progress);The console prints 80.