TypeScript 読み取り専用の配列

宣言した配列を読み取り専用にしたいときには ReadonlyArray が使えます。

普通の配列の場合、push をすることができます。

const hoge: number[] = [1, 2, 3];
hoge.push(4);

ReadonlyArray を指定すると push できなくなります。

const hoge: ReadonlyArray<number> = [1, 2, 3];
hoge.push(4); // Property 'push' does not exist on type 'readonly number[]'.