中級 › readonly配列にpushしようとするとdoes not exist on typeになる理由 › パターン1
readonly配列にpushしようとするとdoes not exist on typeになる理由
readonly number[]のように宣言した配列型には、push・pop・splice等の破壊的(要素を変更する)メソッドがそもそも型として存在しません。
1 function printAll(items: readonly number[]): void { 2 items.______(4); ^ 3 console.log(items); 4 } 5 printAll([1, 2, 3]);
main.ts(2,9): error TS2339: Property 'push' does not exist on type 'readonly number[]'.
空欄を埋めてコンパイルを通す
readonly number[]にはpushのような破壊的メソッドは型として存在しません。concatのような非破壊的メソッドを使います。
次の問題