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

idea: Omit/PickDeep type #343

Closed
osdiab opened this issue Jan 6, 2022 · 4 comments · Fixed by #816
Closed

idea: Omit/PickDeep type #343

osdiab opened this issue Jan 6, 2022 · 4 comments · Fixed by #816
Labels
help wanted Extra attention is needed type addition

Comments

@osdiab
Copy link

osdiab commented Jan 6, 2022

In my codebase we ended up with a type like this:

props: Omit<
    FilterableSelectProps<{
      value: string;
      text: string;
    }>,
    "useComboboxProps"
  > & {
    useComboboxProps: Omit<
      FilterableSelectProps<{
        value: string;
        text: string;
      }>["useComboboxProps"],
      "itemToString"
    >;
  };

The idea is to omit from FilterableSelectProps<{value: string, text: string}> the value at useComboboxProps.itemToString but expressing that ends up with a pretty hard to read type. Would be nice to have deep versions of Omit and Pick.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

👍

@sindresorhus sindresorhus added help wanted Extra attention is needed type addition labels Jan 6, 2022
@andrewmclagan
Copy link

Would love to see this type.

@fmerlett-globality
Copy link

Looks like it's implemented decently soundly here (appears to still function in TS 4.8):

Link to SO answer featuring this:

Copypasta of code, for completeness:

/** 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
}>

Copy link
Owner

Thank you @Emiyaaaaa for contributing to close this issue! ⭐

The rewards from this issue, totalling $130, has been shared with you.

What now?

  1. Create a Polar account
  2. See incoming rewards & setup Stripe to receive them
  3. Get payouts as backers finalize their payments

If you already have a Polar account setup, you don't need to do anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants