Course contentsShow
← TypeScript
Lesson 812 of 1,79618. Conditional TypesFree lesson

The DeepPartial Pattern

Recursively apply Partial to nested object properties using conditional types to detect and handle object types.

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:

  1. Conditional check: T extends object asks "Is this type an object?"
  2. Recursive mapping: If yes, map over all keys with [K in keyof T], make each optional (?), and recursively call DeepPartial<T[K]> on the property's type
  3. Base case: If not an object (primitives, functions, etc.), return T unchanged
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.