Skip to content

Instantly share code, notes, and snippets.

@navix
Last active August 4, 2023 09:02
Show Gist options
  • Save navix/6c25c15e0a2d3cd0e5bce999e0086fc9 to your computer and use it in GitHub Desktop.
Save navix/6c25c15e0a2d3cd0e5bce999e0086fc9 to your computer and use it in GitHub Desktop.
TypeScript Deep Partial Interface

TypeScript Deep Partial Interface

export type DeepPartial<T> = T extends Function ? T : (T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T);

Before typescript@3.1

type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends Array<infer U>
    ? Array<DeepPartial<U>>
    : T[P] extends ReadonlyArray<infer U>
      ? ReadonlyArray<DeepPartial<U>>
      : DeepPartial<T[P]>
};

@chuasonglin1995
Copy link

For me, I need that Partial<T[K]> instead of T

export type DeepPartial<T> = {
  [K in keyof T]?: T[K] extends Record<string, unknown> ? DeepPartial<T[K]> : Partial<T[K]>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment