Skip to content

Explicitly encode keyof behaviors for never and unknown into getIndexType #30753

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

Merged
merged 3 commits into from
Apr 16, 2019

Conversation

weswigham
Copy link
Member

keyof never -> string | number | symbol (changed from never)
keyof unknown -> never (unchanged, but no longer derived from unknown's empty apparent type)

The reasoning for the first is pretty simple - we know keyof is contravariant, so the more specialized the input type is, the broader the output type is. For each key we add on the input type, we specify a smaller and smaller set of inputs, while the output set of keys grows. Once the input is the empty set of types (never), our result should encompass every possible key.

Fixes #30657

@weswigham
Copy link
Member Author

@typescript-bot test this & @typescript-bot run dt - I honestly have no idea what this will affect in the wild. Historically people haven't really relied on incorrect boundary relations in TS, so we shall see.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 4, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at d147a0d. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 4, 2019

Heya @weswigham, I've started to run the Definitely Typed test suite on this PR at d147a0d. You can monitor the build here. It should now contribute to this PR's status checks.

@jack-williams
Copy link
Collaborator

This is great change IMO. One semi-related question. Is there a reason for the following, mainly regarding number:

type A = keyof Object; //  "constructor" | "toString" | "toLocaleString" | "valueOf" | "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable"
type B = keyof number; //  "toString" | "toFixed" | "toExponential" | "toPrecision" | "valueOf" | "toLocaleString"
type C = keyof object; //  never;

@weswigham
Copy link
Member Author

@RyanCavanaugh @ahejlsberg no DT or RWC changes 😸

@weswigham weswigham merged commit d405662 into microsoft:master Apr 16, 2019
@weswigham weswigham deleted the keyof-never branch April 16, 2019 01:01
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

Successfully merging this pull request may close these issues.

Type simultaneously does and does not have property
3 participants