-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Explicit string indexing access of map type #26974
Comments
In TypeScript, when a type can be indexed by |
@RyanCavanaugh oh okay :S Thanks for that, it was certainly unexpected behaviour for me. Is there a way to explicitly say its going to be a string? |
On the other hand: type R = Record<string, number>
type Y = keyof R // string It caught me by surprise too. 🌷 |
@unional yep Record is the way to do it I think 👍 |
Worth pointing out that the example in this issue is almost the exact example used in the docs here That example claims that the keys will be of type Assuming I didn't miss something, I think those docs could use an update. |
@naddeoa you are correct the docs are incorrect.. They say this:
But its not true: I would submit a PR for the docs but I dont think I understand the reason why this doesnt work enough to comment on it. IMO its counter-intuitive and we shouldnt need a Record to do this which is why I opened this issue in the first place but shrug |
TypeScript Version: 3.1.0-dev.201xxxxx
Search Terms:
string index, explicit index
Code
Expected behavior:
The type of X to be
string
as I have explicitly said it will be.Actual behavior:
The type of X is
string | number
.Playground Link:
http://www.typescriptlang.org/play/#src=type%20EventDefinitions%3CTEventPayload%20extends%20object%3E%20%3D%20%7B%0D%0A%20%20%20%20%5BeventName%3A%20string%5D%3A%20TEventPayload%3B%0D%0A%20%20%7D%3B%0D%0A%0D%0Atype%20X%20%3D%20keyof%20EventDefinitions%3Cobject%3E%3B
Related Issues:
Maybe this: #26470 ?
The text was updated successfully, but these errors were encountered: