TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
readonly modifier in TypeScript is a compile-time constraint applied to properties within classes, interfaces, and type aliases that prevents reassignment of the property’s reference after its initial assignment. It enforces immutability at the property level during static type checking.
Syntax in Classes
When applied to a class property, areadonly field can only be initialized in two places:
- Inline at the point of declaration.
- Inside the class constructor.
TS2540).
Syntax in Interfaces and Type Aliases
The modifier can be prefixed to properties defining object shapes. Once an object literal is cast to or inferred as this type, the compiler restricts reassignment of those specific properties.Technical Characteristics
1. Shallow Immutability Thereadonly modifier only protects the binding (the reference) of the property. If the property holds a reference type (like an object or an array), the internal state of that object can still be mutated. It does not imply deep immutability.
readonly is strictly a TypeScript construct. It is completely erased during transpilation to JavaScript. It does not emit Object.defineProperty with writable: false, nor does it invoke Object.freeze(). Consequently, it provides zero runtime protection against reassignment.
3. Structural Typing Bypasses
Because TypeScript uses structural typing, a readonly property can be bypassed if the object is aliased to a type that does not enforce the readonly modifier.
Related Constructs
Readonly<T>Utility Type: A mapped type that iterates over all properties of typeTand applies thereadonlymodifier to them.readonlyArrays/Tuples: Applied to array types (e.g.,readonly string[]orReadonlyArray<string>) to remove mutating methods likepush,pop, andsplicefrom the type definition.
Master TypeScript with Deep Grasping Methodology!Learn More





