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

DistributedOmit may not need to constraint the second type argument #866

Open
tommytroylin opened this issue Apr 18, 2024 · 6 comments
Open

Comments

@tommytroylin
Copy link

tommytroylin commented Apr 18, 2024

since typescript omit is something like this

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

could we change the constraint extends KeysOfUnion<ObjectType> in DistributedOmit as same ?

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

Why?

@tommytroylin
Copy link
Author

tommytroylin commented Apr 19, 2024

@sindresorhus

we were build a component wrapper which disallow passing some specific props directly

the Omit in typescript works great until some one come in with a union typed props

here is the demo

import type { DistributedOmit } from 'type-fest';

type DistributedOmitLoose<
  ObjectType,
  KeyType extends keyof any,
> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;

type SomeComponentPropsTypeWithControl = {
  onChange: (value: any) => void;
  controllable: true;
};

type SomeComponentPropsTypeWithoutControl = {
  controllable: false;
};

type SomeComponentProps =
  | SomeComponentPropsTypeWithoutControl
  | SomeComponentPropsTypeWithControl;

// works
type Result1 = Omit<SomeComponentProps, 'value'>;


// works and exactly what i want
type Result2 =
  | Omit<SomeComponentPropsTypeWithControl, 'value'>
  | Omit<SomeComponentPropsTypeWithoutControl, 'value'>;

// loose constraint. works. same as Result2
type Result3 = DistributedOmitLoose<SomeComponentProps, 'value'>;

// DistributedOmit from package
// Type '"value"' does not satisfy the constraint 'keyof SomeComponentPropsTypeWithoutControl'
type Result4 = DistributedOmit<SomeComponentProps, 'value'>;

IMO, when omitting properties from a interface, there's no need to check if they were actually exist.
That's why TS's Original Omit didn't check the 2nd type argument. (Just my opinion, not checked if it's right)

@sindresorhus
Copy link
Owner

I do agree that it would be nice to support this use-case. One downside is that it then would not be able to auto-complete the key.

@sindresorhus
Copy link
Owner

@henriqueinonhe Thoughts?

@Nr9
Copy link

Nr9 commented Apr 24, 2024

I have a similar issue with DistributedOmit

The simple following type does not work

type ReplaceKey<T, V> = T extends { key: string } ? DistributedOmit<T, 'key'> & { key: V } : T

I get the following error

TS2344: Type "key" does not satisfy the constraint KeysOfUnion<T>

@medv
Copy link

medv commented May 14, 2024

DistributedOmitLoose

This is fantastic, thank you. In a heavily abstracted layer of framework code, it's most often not possible to get autocompletion on Omit anyway. This is a great drop in replacement for DistributedOmit.

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

5 participants