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