utility-types icon indicating copy to clipboard operation
utility-types copied to clipboard

DeepOmit<T, K> type

Open davydkov opened this issue 4 years ago • 2 comments

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.

davydkov avatar Dec 19 '19 13:12 davydkov

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?

piotrwitek avatar Dec 19 '19 23:12 piotrwitek

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?

cdaringe avatar Jul 12 '23 23:07 cdaringe