What is the output type of the following? type DeepReadonly = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly : T[K] };

TypeScript Associate Hard

TypeScript Associate — Hard

What is the output type of the following? type DeepReadonly = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly : T[K] };

Key points

  • DeepReadonly creates a deep readonly type by recursively applying readonly to all nested object properties.
  • This ensures that all levels of nested objects are readonly, not just the top level.
  • The key in keyof T syntax allows for iterating over all keys in T to apply the readonly modifier.

Ready to go further?

Related questions