Last active
January 11, 2022 08:38
-
-
Save karladler/2e6860504c841833521fbdb9cc0076d7 to your computer and use it in GitHub Desktop.
Typescript helpers for building interfaces
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
/** | |
* Make all properties partial and then pick only some that are required: | |
* https://stackoverflow.com/questions/52703321/make-some-properties-optional-in-a-typescript-type | |
*/ | |
export type OptionalExceptFor<T, TRequired extends keyof T> = Partial<T> & Pick<T, TRequired> | |
/** | |
* Type XOR | |
*/ | |
type Diff<T, U> = T extends U ? never : T; | |
/** | |
* Make all properties required except certain fields. | |
*/ | |
export type RequiredExceptFor<T, TOptional extends keyof T> = Pick<T, Diff<keyof T, TOptional>> & Partial<T>; | |
/** | |
* Optional makes all object fields optional | |
*/ | |
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>; | |
/** | |
* Deep Partial makes all object fields optional recursively | |
*/ | |
export type DeepPartial<T> = { | |
[P in keyof T]?: DeepPartial<T[P]>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment