Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Literal and Object Types

TypeScript readonly properties

Reference for readonly properties in TypeScript: assignment rules, constructors, shallow readonly, ReadonlyArray, Readonly<T> and what it does not enforce at runtime.

The readonly modifier on a property tells the compiler that the property cannot be assigned to after the object is created. It is a compile-time check only: the emitted JavaScript is unchanged, and nothing stops untyped code from writing to the property.

You can use readonly on interface and type members, class fields, index signatures, array and tuple types (readonly string[]) and, through Readonly<T>, on every property of a type at once. It is the main tool for documenting that a value is meant to be treated as immutable.

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

Try it

TypeScript Readonly in objects and classes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Assigning to a readonly property

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
interface A { readonly id: number }
class B { readonly id = 1; constructor(readonly name: string) {} }
type C = Readonly<A>;
let list: readonly string[] = ["a"];
let tuple: readonly [number, number] = [1, 2];
type D = { readonly [key: string]: number };
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
Form Description
readonly p: T The property cannot be assigned to, except in the declaration and (for class fields) the constructor.
readonly T[] / ReadonlyArray<T> An array type without push, splice or element assignment.
Readonly<T> Utility type that adds readonly to every property of T.
-readonly In a mapped type, removes the modifier.

Errors: assigning to a readonly property is error 2540 ("Cannot assign to 'x' because it is a read-only property").

Usage notes

  • The second example fails on purpose and prints the real compiler error.
  • readonly is shallow. A readonly property that holds an array or object still lets you change the inside of that value. Combine it with readonly string[] or a deep readonly utility for deeper protection.
  • Readonly does not affect assignability between object types: a mutable object can be assigned to a type with readonly properties, and the reverse is allowed too. The compiler treats the modifier as intent, not as a different type.
  • A class field marked readonly can be assigned in its initializer or in the constructor. Parameter properties (constructor(readonly name: string)) declare and assign in one step.
  • as const makes object literals deeply readonly and keeps literal types.
  • Getter-only accessors are also treated as readonly by the compiler.
  • For real runtime protection use Object.freeze, which Readonly<T> matches in type form (its return type is Readonly<T>).
  • Function parameters can be annotated readonly T[] to promise you will not mutate the caller's array.