TypeScript·Intermediate·Coding·1 min read
What does Readonly<T> protect against, and what doesn't it protect against?
Short interview answer
It marks all of T's properties as readonly at the type level, so the compiler flags reassignment attempts. It provides no runtime immutability — a caller who bypasses the type system or receives the object through untyped code can still mutate it.
Example
const cfg: Readonly<{ url: string }> = { url: '/api' };cfg.url = '/other'; // compile error(cfg as any).url = '/other'; // no runtime protectionKey takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.