How do you implement a deep partial type in TypeScript?

Answer

The built-in Partial<T> only makes top-level properties optional. A DeepPartial recursively makes all nested properties optional as well. Implementation using conditional types and mapped types: type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T;. This checks if T is an object type — if so, it maps over keys making each optional and recursively applying DeepPartial. If T is a primitive, it returns T as-is. For more robustness (handling arrays, functions, dates): type DeepPartial<T> = T extends Function | Date | RegExp ? T : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T;. Use cases: deeply nested update objects (e.g., updating nested config), test factories where you want to override only specific nested properties, state management where partial updates can apply at any depth.