Readonly Arrays

Arrays That Cannot Change

readonly string[] (or ReadonlyArray<string>) removes push, pop, splice and index assignment. Use it for data that must not be changed, such as configuration or function inputs you only read.

Example

Methods that return a new array (map, filter, slice) still work.

const PAYMENT_METHODS: readonly string[] = ["UPI", "Card", "NetBanking", "COD"];

const online = PAYMENT_METHODS.filter((m) => m !== "COD");
console.log(online);
console.log(PAYMENT_METHODS.includes("UPI"));

Output

[ 'UPI', 'Card', 'NetBanking' ]
true

Trying to Modify

Example

Both lines fail: TS2339 (push does not exist on a readonly array) and TS2542 (index signature only permits reading).

const PAYMENT_METHODS: readonly string[] = ["UPI", "Card"];
PAYMENT_METHODS.push("Crypto");
PAYMENT_METHODS[0] = "Cash";

Compiler output (tsc)

Line 2: error TS2339: Property 'push' does not exist on type 'readonly string[]'.
Line 3: error TS2542: Index signature in type 'readonly string[]' only permits reading.

Note: readonly is a compile-time check only. At runtime it is still a normal array. Use Object.freeze if you need runtime protection too.