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;- ✓Correct answer: With a recursive mapped type that adds
?at every level - 2With the built-in utility
Partial<T, 'deep'>from lib.d.ts - 3You can't: recursive type aliases are forbidden in TypeScript
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.