-
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
Wildly unpredictable behavior for resolving number-type index expressions #57296
Comments
I wouldn't say our behavior is wildly unpredictable, but it is perhaps a bit surprising in some cases. These are the rules:
Aspects of these rules that are somewhat surprising:
Overall, it's not clear to me that anything needs to change here. |
Wow. That makes so much more sense when you explain it like that! The round-trip issue isn't a big problem, but did hinder me trying to grok the actual behavior. The only thing I think is an important bug is that
The implementation doesn't quite match the description you gave. |
I think most people's mental model is that if they have The reverse interpretation - that |
That doesn't match my mental model, but perhaps I'm in the minority. I would expect that if something is number-parseable, you have already parsed it! Anyway, thank you for explaining. This is clearly at least somewhat by design and I think my issues are covered by #46109 and #57404. Though the docs definitely could clarify what |
Regarding
It should say "non-empty string literal type". I've modified the original comment to reflect this. |
π Search Terms
numeric index property access
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/dev/bug-workbench/?target=9#code/PTAEAEBcEMCcHMCmkBcBRAygJgAxawFAAmiAxgDZyKikD2AdgM6Si2gqgDeBovoA2gA8UzWAEt68ALooA5KInxZAH1n0ArgFsARolgq1WhZNkAaHnyEoNOvTMO39qm8aXm+A4QAMAJJxu6sAC+XvYukOImBEEEdEwsAA4AjKAAvKz8SVIEIB4AegD8sQzMoAlYaRmySbLZuXyFxfFlAMyVtPyyDIi1OWANRXGlCQAs7Z04gji99byNQ4kArOOyAJL0AGYSYpAAnjP9c4MliQBs476cLTg49CF1h6DzJ2UA7CtJWC0A+iOLpwd8sdmgkABzjT4-P6nB5AprDACcKwAtDVYQN4YkkjgVgBqNF9OELMpJFLpDqXfgw+6EgZAA
π» Code
π Actual behavior
The inferred types of these index expressions are very unpredictable.
The indexes
'1'
,'0x0'
,`${300n}`
,-1
,+1
are inferred as`${number}`
, and NOT as 'number'.The indexes
1
,'Infinity'
,123_456
, are inferred asnumber
The indexes
'one'
,'123_456'
,'${[6]}'
are inferred asstring
(these are understandable and not a problem)π Expected behavior
I expect:
number
and${number}
to catch the same cases, since (I believe) they express the same intent as object keys. Likely they should be considered a duplicate index signature in the type declaration.'1'
and1
to be treated the same since they retrieve the identical property.+1
and0x0
to be inferred as non-numeric string, since (I think) the${number}
is supposed to be "the strings that a number may coerce to", not "the strings which are valid numeric literals". (though it's curious that strings containing binary, octal, and hex literals are accepted by the template string but NOT digit separators'123_456'
)${300n}
to be inferred as a non-numeric string. It's odd that it is treated as the string literal"300"
in the index but a non-literalstring
when initializing a const variable.Additional information about the issue
Originally mentioned here.
This can surely be split into multiple issues.
The text was updated successfully, but these errors were encountered: