-
-
Save jiverson/97ad96b6277dfe4b434d3c83994ad8fb to your computer and use it in GitHub Desktop.
Deeply omit members of an interface or type
This file contains hidden or 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
// Taken from https://stackoverflow.com/questions/55539387/deep-omit-with-typescript | |
/** Union of primitives to skip with deep omit utilities. */ | |
type Primitive = string | Function | number | boolean | Symbol | undefined | null | |
/** Deeply omit members of an array of interface or array of type. */ | |
export type DeepOmitArray<T extends any[], K> = { | |
[P in keyof T]: DeepOmit<T[P], K> | |
} | |
/** Deeply omit members of an interface or type. */ | |
export type DeepOmit<T, K> = T extends Primitive ? T : { | |
[P in Exclude<keyof T, K>]: //extra level of indirection needed to trigger homomorhic behavior | |
T[P] extends infer TP ? // distribute over unions | |
TP extends Primitive ? TP : // leave primitives and functions alone | |
TP extends any[] ? DeepOmitArray<TP, K> : // Array special handling | |
DeepOmit<TP, K> | |
: never | |
} | |
/** Deeply omit members of an array of interface or array of type, making all members optional. */ | |
export type PartialDeepOmitArray<T extends any[], K> = Partial<{ | |
[P in Partial<keyof T>]: Partial<PartialDeepOmit<T[P], K>> | |
}> | |
/** Deeply omit members of an interface or type, making all members optional. */ | |
export type PartialDeepOmit<T, K> = T extends Primitive ? T : Partial<{ | |
[P in Exclude<keyof T, K>]: //extra level of indirection needed to trigger homomorhic behavior | |
T[P] extends infer TP ? // distribute over unions | |
TP extends Primitive ? TP : // leave primitives and functions alone | |
TP extends any[] ? PartialDeepOmitArray<TP, K> : // Array special handling | |
Partial<PartialDeepOmit<TP, K>> | |
: never | |
}> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment