Skip to content

Instantly share code, notes, and snippets.

@VehpuS
Created July 6, 2021 13:02
Show Gist options
  • Save VehpuS/1c50812a6eb705f62e20df5d54f8f420 to your computer and use it in GitHub Desktop.
Save VehpuS/1c50812a6eb705f62e20df5d54f8f420 to your computer and use it in GitHub Desktop.
DeepPartial type - allow undefined / null instantiation of nested fields in a Typescript type (implemented before I found standard implementations + good practice :) ).
export type DeepPartial<T> = {
[P in keyof T]?: NonNullable<T[P]> extends Array<infer U>
? Array<DeepPartial<NonNullable<U>>> | null
: NonNullable<T[P]> extends ReadonlyArray<infer V>
? ReadonlyArray<DeepPartial<NonNullable<V>>> | null
: NonNullable<T[P]> extends Date
? Date
: NonNullable<T[P]> extends {}
? DeepPartial<NonNullable<T[P]>> | null
: NonNullable<T[P]> | null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment