TypeScript · Senior · Question 6 of 75

How do you implement DeepPartial<T>?

type DeepPartial<T> = T extends object
  ? { [K in keyof T]?: DeepPartial<T[K]> }
  : T;

Explanation

Type aliases can reference themselves. A real implementation should handle arrays, functions and Date separately so they aren't "broken down" into properties. Deep recursion has an instantiation limit, so huge types slow down the compiler.

Further reading

Problem with a question

TypeScript · Senior · question 6

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz