Sub topic 7
Why `readonly` Does Not Affect Assignability
Assume you know basic TypeScript syntax, interfaces, type aliases, union types, and JavaScript objects. This chapter focuses on TypeScript’s compile-time rules, not runtime immutability.
The readonly modifier prevents assignment through a particular typed reference:
interface FrozenPoint {
readonly x: number;
readonly y: number;
}
const point: FrozenPoint = { x: 1, y: 2 };
// point.x = 5; // Error: Cannot assign to 'x' because it is a read-only property.The object still contains ordinary JavaScript properties at runtime. TypeScript only checks whether code tries to write through a reference whose type marks the property as read-only.
Assignability ignores the write restriction
TypeScript uses structural typing. A type is usually assignable to another type when it has the required properties with compatible value types.
For assignability, TypeScript checks that both FrozenPoint and MutablePoint have an x property containing a number. It does not reject the assignment because one version has readonly.
interface MutablePoint {
x: number;
y: number;
}
interface FrozenPoint {
readonly x: number;
readonly y: number;
}
const frozen: FrozenPoint = { x: 1, y: 2 };
const mutable: MutablePoint = frozen; // Allowed
mutable.x = 10; // Allowed through the mutable reference
console.log(frozen.x); // 10Both variables refer to the same object. The frozen variable cannot write to x, but mutable can. The readonly restriction belongs to the type of the reference, not permanently to the object.
The reverse assignment is also allowed:
const ordinary: MutablePoint = { x: 3, y: 4 };
const viewOnly: FrozenPoint = ordinary; // Allowed
// viewOnly.x = 8; // Error through this reference
ordinary.x = 8; // Allowed through the other referenceThis direction is useful because a function that only reads its argument can accept mutable data without requiring callers to copy it.
Why TypeScript makes this trade-off
A strict rule would make many normal APIs harder to use. For example, a function that promises not to modify an array should accept both mutable and read-only arrays.
function total(values: readonly number[]): number {
return values.reduce((sum, value) => sum + value, 0);
}
const numbers = [2, 4, 6];
console.log(total(numbers)); // 12
const fixed = [1, 3, 5] as const;
console.log(total(fixed)); // 9The parameter type communicates the function’s behavior to its implementation. Inside total, methods such as push are unavailable. Callers do not need to convert their mutable arrays into new arrays first.
TypeScript chooses convenience and structural compatibility over a complete guarantee that no alias can ever mutate the value.
The protection is shallow
For an object property, readonly prevents replacing the property value. It does not recursively protect the value stored in that property.
interface Configuration {
readonly tags: string[];
}
const config: Configuration = { tags: ["stable"] };
config.tags.push("fast"); // Allowed
// config.tags = []; // Error: tags is read-only
console.log(config.tags); // ["stable", "fast"]To prevent changes to the array through this type, mark the array itself as read-only:
interface SaferConfiguration {
readonly tags: readonly string[];
}
const safer: SaferConfiguration = { tags: ["stable"] };
// safer.tags.push("fast"); // Error: push does not exist
// safer.tags = []; // Error: tags is read-onlyThe first readonly protects the property slot. The second protects the array value exposed through that property.
Readonly references and aliases
The relationship can be pictured as two references pointing to one object:
When designing an API, use readonly to state that an implementation does not need to mutate its input. Do not treat it as a security boundary or as proof that data cannot change. Use runtime techniques such as Object.freeze when runtime protection is required, and remember that Object.freeze is also shallow unless nested values are handled separately.
| Type declaration | What it prevents |
|---|---|
| `readonly name: string` | Reassigning `name` through that reference |
| `readonly items: string[]` | Reassigning `items`, but not changing the array |
| `readonly items: readonly string[]` | Reassigning `items` and mutating the exposed array |
Exercise
Why does const mutable: { x: number } = frozen compile when frozen has type { readonly x: number }, and what happens if mutable.x changes?
Show answer
TypeScript checks compatible property types for assignability and ignores the readonly write restriction. Both variables can refer to the same object, so changing mutable.x also changes the value observed through frozen.