03 · TypeScript Type Gymnastics
mediumcodingtypescript~12 min

Implement DeepReadonly<T>

Objective: Combine recursive and conditional types, with special cases for functions and arrays.

Make an object and everything nested inside it readonly. Your code is type-checked with strict mode against hidden assertions. Fix every compiler error and make the assertions pass.

  • Nested object properties become readonly at every depth.
  • Arrays become readonly arrays of deeply readonly elements.
  • Functions and primitives are left unchanged.
DeepReadonly<{ a: { b: number[] } }>
→ { readonly a: { readonly b: readonly number[] } }
TypeScript (strict)

⌘/Ctrl + Enter to type-check · Esc then Tab leaves the editor

Output
> Nothing has run yet.
Interview follow-up questions
  • What happens with Map and Set?
  • Why can deep recursive types slow down the compiler?
  • How is this different from `Object.freeze` at runtime?