TypeScript · Junior · Question 8 of 75
What does the readonly modifier do in { readonly id: number }?
- ✓Correct answer: Forbids reassigning the property at the type level; nothing changes at runtime
- 2Makes the object deeply immutable via
Object.freezeat runtime - 3Makes the property private and inaccessible outside the class
Explanation
readonly is just a compiler check: obj.id = 2 causes an error. It's shallow - nested objects can be changed. At runtime the object is ordinary. For arrays there's readonly string[] / ReadonlyArray<string>, and deep freezing is done with as const or Object.freeze.