QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Deep Partial Type

Converts all properties of a type, including nested objects, into optional.

  • helper-types
  • typedefinition
  • optional

Install

npx shadcn@latest add https://quicksnip.dev/r/deep-partial-type.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

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

Usage

type A = {
    name: string;
    details: {
        age: number;
        address: { city: string; zip: string };
    };
};

type PartialA = DeepPartial<A>;
/*
Type PartialA:
{
  name?: string;
  details?: {
    age?: number;
    address?: { city?: string; zip?: string };
  };
}