Skip to content

Readonly

In one line

The readonly modifier prevents assignment to a property or mutation of an array, enforcing immutability at the type level.

In simple words

The readonly modifier marks a property or array as immutable. TypeScript will error if you try to assign a new value to a readonly property or call mutating methods on a readonly array.

For properties: interface Point { readonly x: number; } means x cannot be reassigned after creation. For arrays: readonly number[] removes push, pop, sort, and other mutating methods.

The Readonly<T> utility type makes all properties readonly at once. Note that readonly is shallow — nested objects can still be mutated. For deep immutability, you need a recursive type.

Example

typescript
interface Point { readonly x: number; readonly y: number; }
const p: Point = { x: 10, y: 20 };
// p.x = 30; // Error: cannot assign to readonly

const arr: readonly number[] = [1, 2, 3];
// arr.push(4); // Error: push does not exist on readonly arrays

Readonly properties and arrays cannot be mutated — TypeScript catches attempts at compile time.

Related terms

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary