-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
type ${number}
cannot be used to index numeric index signature
#41893
Comments
...but should it? |
I recall that I read something about the requirement for |
The round-tripping is not a requirement, so @weswigham 's analysis is correct |
@RyanCavanaugh said
Hmm, seems like it should be, right? I'd expect |
This would imply that |
Wow, my intuition is exactly the opposite; could you articulate why it would be surprising? You must be looking at the class of valid string-to-number inputs, while I'm looking at the class of valid number-to-string outputs, and both of us are thinking "yep, that's the natural meaning of Mine is that template literal types should correspond to (untagged) template literal values. Such template literals can consume numbers and produce strings, but they don't consume strings to produce numbers. If Furthermore, the set of valid number-to-string outputs has a use case we already care about in TypeScript: "numeric" object indices. And while the set of string-to-number inputs might be useful, we don't currently have a string-to-number type function (as in #26382) to use with it. So I'd be interested in seeing use cases where the prohibition of |
I ran a poll and most people seem to have the reverse interpretation https://twitter.com/SeaRyanC/status/1420059847156928512 |
Oh, well, 78.6% of respondents are wrong! Kidding, kind of. I guess let s4: `${number}%` = "0xfacade%"; // okay or error
(arr: string[], idx: `${number}`) => arr[idx]; // okay or error
let s5: `This sentence is ${boolean}` = "This sentence is 0"; // okay or error
let s6: `${100.0}%` = "100.0%"; // okay or error
let s7: `${number}%` = " 333 %"; // okay or error instead. In any case, this really makes me wish we had a different notation for strings-that-are-coercible-to-finite-numbers (maybe a new intrinsic type like |
I think where |
The original issue reported here is fixed by #48837: Numeric index signatures are now applicable for indices of type With respect to whether |
TypeScript Version: 4.2.0-dev.20201209
Search Terms:
Expected behavior:
type
${number}
can be used to index numeric index signatureActual behavior:
Error:
Element implicitly has an 'any' type because index expression is not of type 'number'.
Related Issues:
Code
Playground Link: Provided
The text was updated successfully, but these errors were encountered: