Recursive Partial in TypeScript

With the landing of Conditional Types in 2.8, we can now declare a recursive partial type as follows.

type RecursivePartial<T> = {
  [P in keyof T]?:
    T[P] extends (infer U)[] ? RecursivePartial<U>[] :
    T[P] extends object ? RecursivePartial<T[P]> :
    T[P];
};

Reference:

http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html

Leave a Comment