ProgramGuru

Readonly Class Properties

Understand Readonly Class Properties

A readonly class property can be assigned where initialization is permitted and cannot then be reassigned by ordinary checked TypeScript code.

What you will learn: the purpose of readonly class properties, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how readonly class properties works and where its type rules apply.
  • Predict the exact output of the readonly class properties example.
  • Diagnose a common issue: Do not assume readonly tags: string[] makes the array immutable.

Build the mental model

readonly is a static restriction, not runtime Object.freeze. If a readonly property references a mutable array, the reference cannot be replaced through that property, but the array may still be mutated unless it is also typed or protected as readonly.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Readonly Class Properties: explained visuallyreadonly tags ref → Mutable [] value → push is allowedreadonly tags refMutable [] valuepush is allowedFollow the values through the operation
Readonly Class Properties: readonly tags ref → Mutable [] value → push is allowed.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

class RecordEntry {
  readonly id: number;
  readonly tags: string[];
  constructor(id: number) {
    this.id = id;
    this.tags = [];
  }
}
const entry = new RecordEntry(17);
entry.tags.push("new");
console.log(entry.id);
console.log(entry.tags.join(", "));

Verified output

17
new

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

The constructor assigns the readonly id and tags reference once. push modifies the array stored behind tags without reassigning the property. The result is 17 and new.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not assume readonly tags: string[] makes the array immutable. Use readonly string[] when element mutation must also be forbidden by the checker.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Change tags to readonly string[] and confirm that push is rejected at compile time.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Why does entry.tags.push("new") compile here?
Choose one answer. You can try again.

Answer and explanation

readonly prevents replacing the property reference, not modifying a mutable array it points to.

A readonly class property can be assigned where initialization is permitted and cannot then be reassigned by ordinary checked TypeScript code.

Review the key idea

Readonly Class Properties: A readonly class property can be assigned where initialization is permitted and cannot then be reassigned by ordinary checked TypeScript code.

The constructor assigns the readonly id and tags reference once. push modifies the array stored behind tags without reassigning the property. The result is 17 and new.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Parameter Properties →