A checkout bug I fixed in a Seattle store’s TypeScript code came down to one helper that called sort() on the shared list of shipping states.
Declaring the list with const didn’t help, since const locks the variable, not the items. A TypeScript readonly array stops that at compile time, because its type has no push(), sort() or other mutating methods.
Quick answer: type the array as readonly string[] or ReadonlyArray<string>. Reading, map() and filter() still work, while push(), index writes and sort() turn into compile errors:
const shipStates: readonly string[] = ["CA", "NY", "TX", "WA"];
shipStates.push("FL"); // error TS2339
In this tutorial, I’ll show you how to declare a TypeScript readonly array, what the compiler rejects, how to update or copy one safely, and where readonly protection stops.
Each file ran under strict mode with TypeScript 7.0.2 and Node.js v22.22.2, and the screenshots come straight from VS Code.
How to Declare a Readonly Array in TypeScript
TypeScript gives you two spellings for the same type. readonly string[] is the shorter one (added in TypeScript 3.4), and ReadonlyArray<number> is the generic form:
const shipStates: readonly string[] = ["CA", "NY", "TX", "WA"];
const taxRates: ReadonlyArray<number> = [0.0725, 0.04, 0.0625, 0.065];
console.log(shipStates.length, shipStates.includes("TX"));
console.log((taxRates[2] * 100).toFixed(2) + "%");
const lower = shipStates.map((s) => s.toLowerCase());
lower.push("or"); // map() returns a normal array
console.log(lower, shipStates);
Output:
4 true
6.25%
[ 'ca', 'ny', 'tx', 'wa', 'or' ] [ 'CA', 'NY', 'TX', 'WA' ]
![typescript readonly array declare readonlyarray vs code VS Code running a TypeScript readonly array declared with readonly string[] and ReadonlyArray, then map() into a new array](https://pythonguides.com/wp-content/uploads/2026/11/typescript-readonly-array-declare-readonlyarray-vs-code.png)
map() accepts push() while the original keeps four states.Think of it as TypeScript’s immutable array type. Non-mutating methods stay, and map() returns a normal string[] you can change.
This is different from const, which only stops reassignment. I compare the two in my post on const arrays in TypeScript.
What Errors Do You Get When You Change a Readonly Array?
Same four states. Here’s what happens when code tries to add, overwrite, sort or pass the array to a string[] parameter:
const shipStates: readonly string[] = ["CA", "NY", "TX", "WA"];
shipStates.push("FL");
shipStates[0] = "OR";
shipStates.sort();
function addState(list: string[]): void {
list.push("FL");
}
addState(shipStates);
tsc output:
ro_errors.ts(3,12): error TS2339: Property 'push' does not exist on type 'readonly string[]'.
ro_errors.ts(4,1): error TS2542: Index signature in type 'readonly string[]' only permits reading.
ro_errors.ts(5,12): error TS2339: Property 'sort' does not exist on type 'readonly string[]'.
ro_errors.ts(10,10): error TS4104: The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'.
![typescript readonly array push error ts2339 vs code VS Code showing TypeScript errors for push, index assignment, sort and passing a readonly array to a string[] parameter](https://pythonguides.com/wp-content/uploads/2026/11/typescript-readonly-array-push-error-ts2339-vs-code.png)
push and sort don’t exist on the type (TS2339), and you can’t write to an index (TS2542).
The last one, TS4104, matters most: a readonly array can’t go to a parameter typed string[]. So I type parameters as readonly string[], which accepts both kinds, and I do the same for React props.
splice() is blocked too; see the TypeScript splice method for its copy-based twin, toSpliced().
Sort, Add or Remove Items in a Readonly Array
You never change a readonly array in place. Build a new one with the ES2023 copy methods or spread syntax:
const shipStates: readonly string[] = ["WA", "CA", "TX", "NY"];
const sorted = shipStates.toSorted();
const added = [...shipStates, "FL"];
const swapped = shipStates.with(0, "OR");
const removed = shipStates.filter((s) => s !== "TX");
console.log("sorted: ", sorted);
console.log("added: ", added);
console.log("swapped: ", swapped);
console.log("removed: ", removed);
console.log("original:", shipStates);
Output:
sorted: [ 'CA', 'NY', 'TX', 'WA' ]
added: [ 'WA', 'CA', 'TX', 'NY', 'FL' ]
swapped: [ 'OR', 'CA', 'TX', 'NY' ]
removed: [ 'WA', 'CA', 'NY' ]
original: [ 'WA', 'CA', 'TX', 'NY' ]

toSorted(), toReversed() and with() need lib es2023+ and Node.js 20+. Comparers work like sort(); see sorting arrays in TypeScript.
Convert a Readonly Array to a Mutable Array in TypeScript
To get a mutable array back, copy it. A type assertion also compiles, but it gives you the very same array:
const shipStates: readonly string[] = ["CA", "NY", "TX", "WA"];
// readonly -> mutable: make a copy
const editable: string[] = [...shipStates]; // or .slice()
editable.push("FL");
console.log("copy: ", editable);
console.log("original: ", shipStates);
// a cast compiles, but it is the same array
const unsafe = shipStates as string[];
unsafe.push("OR");
console.log("after cast:", shipStates);
// mutable -> readonly: no copy is made
const locked: readonly string[] = editable;
editable.pop();
console.log("locked: ", locked);
Output:
copy: [ 'CA', 'NY', 'TX', 'WA', 'FL' ]
original: [ 'CA', 'NY', 'TX', 'WA' ]
after cast: [ 'CA', 'NY', 'TX', 'WA', 'OR' ]
locked: [ 'CA', 'NY', 'TX', 'WA' ]
![typescript convert readonly array to mutable vs code VS Code converting a TypeScript readonly array to a mutable array with spread, then mutating it through an as string[] cast](https://pythonguides.com/wp-content/uploads/2026/11/typescript-convert-readonly-array-to-mutable-vs-code.png)
The cast pushed OR into shipStates, the exact bug readonly should stop. The last line shows the reverse trap: assigning to a readonly type makes no copy, so locked changes with editable.
At the type level, a -readonly modifier strips the flag; I explain that syntax in mapped types in TypeScript.
as const Arrays and Readonly Tuples
as const turns an array literal into a readonly tuple of literal types, handy for a fixed list of plan names:
const PLANS = ["basic", "pro", "team"] as const;
type Plan = (typeof PLANS)[number]; // "basic" | "pro" | "team"
function isPlan(value: string): value is Plan {
return (PLANS as readonly string[]).includes(value);
}
const fromUrl: string = "pro";
console.log(PLANS.length, isPlan(fromUrl), isPlan("enterprise"));
Output:
3 true false
(typeof PLANS)[number] gives you the union of the three names. You need the cast in isPlan() because the tuple’s includes() only accepts those literals.
Many teams use this pattern instead of an enum. If you already have enums, here’s how to convert an enum to an array in TypeScript.
Readonly Property vs Readonly Array: Is push() Allowed?
Yes, if only the property is readonly. readonly items: string[] locks the reference, not the contents. You need readonly string[] on the type as well:
class Cart {
readonly items: string[] = [];
}
class LockedCart {
readonly items: readonly string[] = ["Desk lamp"];
}
const cart = new Cart();
cart.items.push("Desk lamp"); // no error here
cart.items = [];
const locked = new LockedCart();
locked.items.push("Monitor");
tsc output:
ro_property.ts(10,6): error TS2540: Cannot assign to 'items' because it is a read-only property.
ro_property.ts(13,14): error TS2339: Property 'push' does not exist on type 'readonly string[]'.
![typescript readonly property array push allowed vs code VS Code showing TypeScript allowing push on a readonly property array but rejecting reassignment and push on a readonly string[] property](https://pythonguides.com/wp-content/uploads/2026/11/typescript-readonly-property-array-push-allowed-vs-code.png)
cart.items.push(); only the reassignment and the push into LockedCart fail.The same rule applies to interface fields. My article on the TypeScript interface for an array of objects shows where to put the modifier.
Why Your TypeScript Readonly Array Still Changes
Two limits catch people: readonly is shallow, so objects inside stay writable, and it’s erased when TypeScript compiles to JavaScript:
interface Order { id: string; total: number }
const orders: readonly Order[] = [
{ id: "A-1001", total: 49.99 },
{ id: "A-1002", total: 120.0 },
];
orders[0].total = 0; // compiles: readonly is shallow
console.log(orders[0]);
const frozen = Object.freeze(["CA", "NY"]);
try {
(frozen as string[]).push("TX"); // cast skips the type check
} catch (err) {
console.log((err as Error).name + ": " + (err as Error).message);
}
Output:
{ id: 'A-1001', total: 0 }
TypeError: Cannot add property 2, object is not extensible

For deep protection, type the items too: readonly Readonly<Order>[]. For runtime protection, use Object.freeze(), which also returns a readonly type.
Freeze is shallow too, so nested data needs a deep clone in TypeScript or a recursive freeze. Python’s closest match is the tuple; see mutable vs immutable in Python.
Still working with TypeScript arrays? These pair well with this one:
- Add elements to an array in TypeScript
- Filter arrays in TypeScript
- Find the length of an array in TypeScript
- Reverse an array in TypeScript
TypeScript Readonly Array FAQs
What’s the difference between readonly string[] and ReadonlyArray?
None. They’re the same type. readonly string[] is the shorter syntax, available since TypeScript 3.4.
Is a readonly array the same as a const array?
No. const stops you from reassigning the variable, but push() still works. readonly blocks changes to the items.
How do I convert a readonly array to a mutable array?
Copy it with [...arr] or arr.slice(). Avoid as string[], which lets you mutate the original.
Does a readonly array stop changes at runtime?
No. It only exists at compile time. Use Object.freeze() if the array must stay unchanged in JavaScript.
Can a tuple be readonly in TypeScript?
Yes. Write readonly [number, number], or add as const to an array literal.
Where is the ReadonlyArray type documented?
See The ReadonlyArray Type in the TypeScript Handbook.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn