Function Annotations

Duration: 10 min  •  Difficulty: Medium

We can provide types for function parameters and return values.

main.ts
// Parameters 'a' and 'b' must be numbers
// The function also returns a number
const multiply = (a: number, b: number): number => {
return a * b;
}
const result = multiply(10, 5);
console.log("Result 10 x 5 =", result);
// multiply("10", 5); // Error! Argument of type 'string' is not assignable to parameter of type 'number'.