Utility Readonly
Question
What does Readonly<T> do?
Hint
Readonly properties cannot be reassigned after initialization.
Explanation
Readonly<T> makes all properties of type T readonly (cannot be reassigned). type Point = { x: number; y: number }. type RPoint = Readonly<Point> makes x and y readonly. Attempting to modify rp.x = 5 is a compile error. Readonly is a compile-time check — it does not prevent mutation at runtime (the JS object is still mutable). For true immutability, use Object.freeze() or immutable data libraries. Readonly is useful for configuration objects, function arguments that should not be modified.
Related Resources
Related Lessons
- Utility Types
Learn utility types.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.