Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

readonly

The readonly property modifier lets an object property be assigned during initialization but prevents later assignments through that property. It is a compile-time, shallow protection rather than runtime deep immutability.

How it works

Prefix an interface property with readonly to make it assignable when the object is created but not writable afterward through that property. This restriction is enforced by TypeScript’s type system, so it does not make the referenced value deeply immutable or alter runtime behavior.

Syntax

readonly propertyName: Type;

Key points

  • Readonly properties express that an object’s selected fields may be set during creation but should not be changed afterward.
  • In an interface, place readonly before the property name in its declaration.
  • Once a readonly object property has been initialized, later assignments to it are rejected by the type checker.
  • ReadonlyArray<T> exposes array contents without mutation methods such as push, pop, or splice.

Common mistakes

  • Use const for variables and readonly for properties; they apply to different declarations.

Example: Readonly interface properties

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

const point: Point = { x: 10, y: 20 };
console.log(point.x, point.y);
// point.x = 15; // Error: cannot assign to a readonly property

The interface permits reading the coordinates while preventing assignments to them during type-checking.

When to use it

Stable object fields

Define configuration or domain objects whose identity fields should not be reassigned after construction.

Read-only array views

Publish APIs that expose collection data without allowing callers to mutate the collection through the returned reference.

Immutable-style interfaces

Model coordinates, identifiers, or other value objects that should remain fixed after creation.

Interview questions

QWhat does readonly do to an interface property?

readonly is a property modifier that prevents assignments to that property during type-checking after initialization. It is appropriate for object properties whose value should not be replaced by consumers.

Follow-up: Does readonly create runtime immutability? No. It is a compile-time restriction and does not change JavaScript runtime behavior.

QHow does readonly differ from const?

const applies to variables, while readonly applies to properties. A const variable cannot be rebound, whereas a readonly property cannot be assigned through that property after creation.

Follow-up: Can a readonly property hold mutable data? Yes; the property reference cannot be replaced, but the referenced value may still be changed unless its own type prevents mutation.

QDoes readonly make an object deeply immutable?

A readonly property is shallow: it blocks rewriting the property itself but does not recursively freeze nested objects. Use readonly array or tuple types when the collection’s mutating operations should be unavailable through that reference.

Follow-up: What type represents an array that should not be mutated? ReadonlyArray<T> provides an array view with mutating methods removed.

  • ReadonlyArray<T>
  • readonly tuples
  • const
  • Readonly<Type>

Source: Handbook - Interfaces. Checked 2026-10-15.