Code Fix

中級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'?

空欄を埋めてコンパイルを通す

広告
広告スロット(未設定)

readonly配列にpushしようとするとdoes not exist on typeになる理由

呼び出そうとした時点で「そのメソッドは無い」というエラーになります。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)