readonly
readonly is a type-level modifier that removes writes from a property, array, or tuple. It does not freeze the value at runtime.
Detailed Explanation
JavaScript has const, Object.freeze, and #private. TypeScript's readonly is none of those. It is a claim the compiler enforces: this property or this index should not be assigned through this type. Another alias of the same object can still mutate it, and the emitted JavaScript has no readonly left on it.
You can mark a property:
interface Config {
readonly apiBase: string;
}
You can mark an index: readonly [index: string]: string. You can mark an array: readonly string[] or ReadonlyArray<string>. You can mark a tuple: readonly [string, number]. The Readonly<T> utility maps every property of T to readonly.
The assignability rule is one-way for collections. A string[] may be passed where readonly string[] is expected, because promising not to write is a weaker requirement. A readonly string[] may not be passed where string[] is expected, because the callee might push. The same idea applies to Readonly<T> versus T for object types whose properties are all covariant reads — with the caveat that methods that mutate this complicate the picture.
readonly on a property is shallow. readonly user: User prevents obj.user = other, not obj.user.name = "x". Deep immutability is a mapped type you write yourself, or a library. as const on a literal produces readonly tuples and readonly properties automatically, and also narrows literals.
const and readonly answer different questions. const prevents rebinding a variable. readonly prevents assigning a property. const user = { name: "Ada" } still allows user.name = "Grace". const user: { readonly name: string } = { name: "Ada" } does not, through that type.
Use readonly on values you intend only to read: function parameters that should not be mutated, exported configuration, and public fields that are set in the constructor. Use Object.freeze when you also need the guarantee after the compiler has been erased. Use #private or private when the issue is visibility, not mutation.
Code Examples
1. Property vs variable: const does not make fields readonly.
// 1. Property vs variable: const does not make fields readonly.
const settings = { theme: "dark" };
settings.theme = "light"; // allowed — the binding is const, the property is not
const frozen: { readonly theme: string } = { theme: "dark" };
// frozen.theme = "light";
// Error: Cannot assign to 'theme' because it is a read-only property.2. One-way assignability for arrays.
// 2. One-way assignability for arrays.
function sum(values: readonly number[]): number {
// values.push(0);
// Error: Property 'push' does not exist on type 'readonly number[]'.
return values.reduce((a, b) => a + b, 0);
}
const mutable = [1, 2, 3];
sum(mutable); // fine
const locked: readonly number[] = [1, 2, 3];
// const copy: number[] = locked;
// Error: readonly array is not assignable to a mutable array.3. Readonly<T> is shallow.
// 3. Readonly<T> is shallow.
type User = { name: string; tags: string[] };
type FrozenUser = Readonly<User>;
const user: FrozenUser = { name: "Ada", tags: ["admin"] };
// user.name = "Grace";
// Error: Cannot assign to 'name' because it is a read-only property.
user.tags.push("editor"); // compiles — the array itself is still mutableCommon Gotchas
`readonly` is erased
any other reference, or any `as` cast to a mutable type, can write. Pair it with `Object.freeze` if runtime immutability matters.
`Readonly<T>` does not freeze nested objects or arrays
mark those fields `readonly` themselves, or write a recursive mapped type.
`readonly` on a class field is a TypeScript-only check
it is not the same as a Java `#final` or a JS `#private` field. The field is still a writable property in the emitted class.
Widening a parameter from `T[]` to `readonly T[]` is safe; the reverse is a breaking change
library authors should accept `readonly` inputs they do not mutate.