Created
July 6, 2021 13:02
-
-
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 :) ).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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