中級 › readonly配列にpushしようとするとdoes not exist on typeになる理由 › パターン2
readonly配列にpushしようとするとdoes not exist on typeになる理由
readonly number[]のように宣言した配列型には、push・pop・splice等の破壊的(要素を変更する)メソッドがそもそも型として存在しません。
1 function clearAll(items: readonly string[]): void { 2 items.______(0, items.length); ^ 3 console.log(items); 4 } 5 clearAll(["a", "b"]);
main.ts(2,9): error TS2551: Property 'splice' does not exist on type 'readonly string[]'. Did you mean 'slice'?
空欄を埋めてコンパイルを通す
spliceは配列を直接書き換える破壊的メソッドです。readonly配列の型には含まれていません。
次の問題