Code Fix

中級readonly配列にpushしようとするとdoes not exist on typeになる理由 › パターン3

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

readonly number[]のように宣言した配列型には、push・pop・splice等の破壊的(要素を変更する)メソッドがそもそも型として存在しません。

 1  function reverseAll(items: readonly number[]): void {
 2    items._______();
               ^
 3    console.log(items);
 4  }
 5  reverseAll([1, 2, 3]);
main.ts(2,9): error TS2339: Property 'reverse' does not exist on type 'readonly number[]'.

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

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

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

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

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

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