The DeepPartial Pattern
What you'll learn: How to build a recursive type that makes all properties optional at every level of a nested object structure.
The Problem with Shallow Partial
You already know Partial<T> makes all properties optional. But it only works one level deep:
interface User {
name: string;
address: {
street: string;
city: string;
};
}
type PartialUser = Partial<User>;
// Result: { name?: string; address?: { street: string; city: string } }
Notice address is optional, but street and city inside it are still required. If address exists, you must provide both nested properties.
Recursively Applying Partial
The DeepPartial pattern uses conditional types to detect object types and recursively apply the transformation:
type DeepPartial<T> = T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T;
How it works:
- Conditional check:
T extends objectasks "Is this type an object?" - Recursive mapping: If yes, map over all keys with
[K in keyof T], make each optional (?), and recursively callDeepPartial<T[K]>on the property's type - Base case: If not an object (primitives, functions, etc.), return
Tunchanged
type DeepPartialUser = DeepPartial<User>;
// Result: { name?: string; address?: { street?: string; city?: string } }
Now every level is optional—you can provide just { address: { city: "NYC" } } without name or street.
Key Takeaway: The DeepPartial pattern uses conditional types to recursively detect object types and apply Partial transformations at every nesting level, creating fully optional deep structures.