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

"Omit" type using "keyof any" instead of "keyof T" #32376

Closed
gins3000 opened this issue Jul 12, 2019 · 1 comment
Closed

"Omit" type using "keyof any" instead of "keyof T" #32376

gins3000 opened this issue Jul 12, 2019 · 1 comment

Comments

@gins3000
Copy link

TypeScript Version: 3.6.0

Search Terms:
Omit
Omit any

Code

interface Foo {
  a: string;
  b: number;
  c: boolean
}

// Omit has the issue
type FooWithoutC = Omit<Foo, "c">; // no intellisense for second generic parameter
type FooWithoutD = Omit<Foo, "d">; // no type error

// Counter example with Pick
type FooWithA = Pick<Foo, "a">; // provides intellisense for filling out second generic parameter
type FooWithD = Pick<Foo, "d">; // type error

Expected behavior:
In the above example, I would expect

  • to get auto-completion for keys of Foo when filling out the second generic parameter
  • to get a type error when putting in a key that does not exist on Foo

similar to how it works in all the other similar helpers, e.g. Pick.

Actual behavior:
Since the definition of the second generic parameter is keyof any instead of keyof T, I can put whatever I want into the second generic parameter, and I also get no Intellisense.

Related line:

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

Playground Link: http://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChnJwBcyAzmFKAOYDc+yARiSAK4C2D0dBCJDmAGwhwQuAL65cAeinIA8m2BhkACzilkYFSmClSLCLjABPAA4oM6AOpKV6FmADCyALzzFYADyWANMgBECP4AfDTIMsggWKCQAgK6ECCkKDDoUGQQCOggACbIVInQwAjIpnBQcGwQkFBGZhaYNlr2YAAiru5K3ph+-jkhYRFRmvXI0FBpkhGO9uDQYwAelaZCyADutsgACsUA1nXmaI22AIIdOwi73ei9cAPhsqYTAG7AORAaMRBxCUkpacgYMAfiAqMgWhksrl8oVKCUyhUqjUDg1rLZ2m4LldfAF+qEHiNDuM0kA

Is there a reason why this has been implemented like this with keyof any instead of keyof T?

@gins3000
Copy link
Author

Never mind, apparently there is already an issue about this: #30825

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

No branches or pull requests

1 participant