Skip to content

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

@gins3000

Description

@gins3000

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?

Activity

gins3000

gins3000 commented on Jul 12, 2019

@gins3000
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gins3000

        Issue actions

          "Omit" type using "keyof any" instead of "keyof T" · Issue #32376 · microsoft/TypeScript