You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug Report] Wrong keyof type with string index signature #39543
I think it's because there's no difference between obj[123] and obj["123"] in JS, and TS won't error in case of orders[orderId] if it's orderId is number (which would be annoying).
Any number converted to string will be valid string, however not every string will be valid number therefore same is not applied for number index signature.
I guess it should be OK to use Extract<keyof T, string> if you don't expect to use numbers there.
Activity
[-]Wrong keyof type with string index signature[/-][+][Bug Report] Wrong keyof type with string index signature[/+]IllusionMH commentedon Jul 10, 2020
This is by design
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#support-number-and-symbol-named-properties-with-keyof-and-mapped-types
meteorlxy commentedon Jul 10, 2020
Andrii Dieiev (@IllusionMH) Thanks for your quick reply. ❤️
What's the underlay reason of this "by design" ? 🤔
It looks odd and causes some strange type checking error in some third party packages
IllusionMH commentedon Jul 10, 2020
I think it's because there's no difference between
obj[123]andobj["123"]in JS, and TS won't error in case oforders[orderId]if it'sorderIdis number (which would be annoying).Any number converted to string will be valid string, however not every string will be valid number therefore same is not applied for
numberindex signature.I guess it should be OK to use
Extract<keyof T, string>if you don't expect to use numbers there.But I can be wrong here.
meteorlxy commentedon Jul 10, 2020
Yeah a possible solution. I'll trying to submit a PR to that package.
Thanks again~ 😄
ExE-Boss commentedon Nov 24, 2020
Arguably, now that we have #40336 and #40598,
keyofshould report`${number}`for numeric index signatures.