utility-types
utility-types copied to clipboard
DeepOmit<T, K> type
Is your feature request related to a real problem or use-case?
Yes, for instance to exclude __typename
field from generated GraphQL type
As a reference - question on stack overflow
Describe a solution including usage in code example
Proposed solution (works for typescript 3.7, but I have not tested with union types)
import { Primitive } from 'utility-types'
type DeepOmitObject<T extends object, K> = {
[P in Exclude<keyof T, K>]: DeepOmit<T[P], K>
}
type DeepOmitArray<T extends any[], K> = {
[P in keyof T]: DeepOmit<T[P], K>
}
export type DeepOmit<T, K> =
T extends Primitive ? T :
T extends object ? DeepOmitObject<T, K> :
T extends Array<infer E> ? Array<DeepOmit<E, K>> :
T extends any[] ? DeepOmitArray<T, K> :
never
Who does this impact? Who is this for?
TypeScript users.
Hey, thanks for the suggestion. Use-case seems legit and the type is not too crazy so I would opt to include it.
What could be possible caveats? What about union types?
Greetings! Came here just to see if it had been added! I have a fn that depletes/consumes content out of an object, and want the returned value to reflect that. seems like robustness concerns have been lightly discussed and covered here: https://stackoverflow.com/questions/55539387/deep-omit-with-typescript
any hinderences?