ReviseAlgo Logo

Classes in TypeScript

Readonly Properties

Prevent mutation of class fields after initialization using the readonly modifier.

Last Updated: July 29, 2026 10 min read

The readonly modifier ensures a field can only be assigned during declaration or inside the class constructor.

1. Introduction & Architecture

2. Deep Dive & Core Concepts

Readonly prevents reassignment of the property reference itself, but does not deeply freeze object properties inside reference types.

3. Basic Code Example

4. Advanced Production Patterns

5. Interactive Code Playground

class Config { readonly env = "production"; }
const c = new Config();
console.log(c.env);

6. Common Pitfalls & Edge Cases

Warning: readonly does not make objects deeply immutable. For deep immutability, use Object.freeze() or Readonly utility types.

7. Interview Q&A & Quizzes

Q: Can a readonly class property be modified inside a constructor?

A: Yes, constructors are the only place where readonly fields can be assigned or updated.

8. Summary Comparison Table

ContextAssignment Allowed
DeclarationYes
ConstructorYes
Class MethodsNo
| External Code | No |