Readonly<T>

Constructs a type with all properties of T set to readonly.

Since TS 2.1 Spec ↗

Syntax

Readonly<Type>

Returns

object type — A new type identical to T but with every property marked readonly.

Examples

interface Point {
  x: number;
  y: number;
}

const origin: Readonly<Point> = { x: 0, y: 0 };
// origin.x = 1; // Error: Cannot assign to 'x'
function freeze<T>(obj: T): Readonly<T> {
  return Object.freeze(obj);
}

Notes

Adds the `readonly` modifier via a homomorphic mapped type. The protection is compile-time only and shallow; nested objects can still be mutated. For deeply immutable structures build a recursive type or use `Object.freeze` at runtime.

See also