Funkcje w TypeScript
Przy deklarowaniu parametrów funkcji również możemy określić ich typ. Spójrz na przykład:
typescriptfunction func(a: number, b: number) {
return a + b;
}
Można również określić typ wartości zwracanej przez funkcję. Zróbmy to:
typescriptfunction func(a: number, b: number): number {
return a + b;
}
Задание 1
Określ typ wyniku i parametrom funkcji:
javascriptfunction sum(x, y) {
return x + y;
}
Задание 2
Określ typ wyniku i parametrom funkcji:
javascriptfunction sum(arr) {
let res = 0;
for (let num of arr) {
res += num;
}
return res;
}