Functions in TypeScript
When declaring function parameters, we can also specify their type. See the example:
function func(a: number, b: number) {
return a + b;
}
You can also specify the return type of the function. Let's do that:
function func(a: number, b: number): number {
return a + b;
}
Specify the type of the function result and parameters:
function sum(x, y) {
return x + y;
}
Specify the type of the function result and parameters:
function sum(arr) {
let res = 0;
for (let num of arr) {
res += num;
}
return res;
}