TypeScriptのfor-ofループ
配列の要素を反復処理する必要がある場合、
私たちはfor-ofループを利用します。
しかし、TypeScriptには一つのニュアンスがあります -
このループ内の要素用の変数の型は明示されません。
なぜなら、TypeScriptは自動的にそれを推論できるからです:
let arr: number[] = [1, 2, 3, 4, 5];
for (let elem of arr) {
console.log(elem);
}
以下のコードをTypeScriptで書き直してください:
let arr = [1, 2, 3, 4, 5];
let res = 0;
for (let elem of arr) {
res += elem;
}
console.log(res);