Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepOmit<T, K> type #129

Open
davydkov opened this issue Dec 19, 2019 · 2 comments
Open

DeepOmit<T, K> type #129

davydkov opened this issue Dec 19, 2019 · 2 comments

Comments

@davydkov
Copy link

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.

@piotrwitek
Copy link
Owner

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?

@cdaringe
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants