What does `type DeepReadonly = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly : T[K] }` accomplish?

TypeScript Professional Hard

TypeScript Professional — Hard

What does `type DeepReadonly = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly : T[K] }` accomplish?

Key points

  • DeepReadonly makes all properties and nested properties readonly
  • It doesn't just apply to top-level properties
  • This is useful for ensuring immutability in complex nested data structures

Ready to go further?

Related questions