Skip to content

Change 'keyof T' to always be string-like #12425

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 7 commits into from
Nov 21, 2016
Merged

Change 'keyof T' to always be string-like #12425

merged 7 commits into from
Nov 21, 2016

Conversation

ahejlsberg
Copy link
Member

This PR revises keyof T to always be a subtype of string vs. a subtype of string | number. This more accurately reflects how properties work in JavaScript and allows us to use keyof T as the inferred type of a for...in variable when the object is of a type parameter type.

This change means that numeric index signatures are ignored by the keyof T operator and that it is never possible to use numbers as values for a keyof T.

Fixes #12314.

@ahejlsberg
Copy link
Member Author

@mhegazy Ping!

@ahejlsberg ahejlsberg merged commit 77c0540 into master Nov 21, 2016
@ahejlsberg ahejlsberg deleted the keyofOnlyStrings branch November 21, 2016 23:51
ethanresnick added a commit to ethanresnick/TypeScript that referenced this pull request Nov 27, 2016
Now that keyof T only returns string or string subtypes (microsoft#12425),
there’s no need to do keyof T & string.
ethanresnick added a commit to ethanresnick/TypeScript that referenced this pull request Nov 28, 2016
Removes number from all the keyof types, and adds a test that numeric
indexes are ignored.
mhegazy added a commit that referenced this pull request Nov 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants