-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Do not allow surrounding spaces to be counted as part of ${number} templates in template index strings #46109
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
Comments
Can be simplified to const a: `data-${number}` = "data-1"; // OK
// No errors
const a1: `data-${number}` = "data- 1";
const a2: `data-${number}` = "data-1 ";
const a3: `data-${number}` = "data- 1 ";
const a4: `data-${number}` = "data- "; |
Oh wow this an even better example for my list of surprising behaviors of |
@jcalz - Thanks for link to 7.1.4.1.2 Runtime Semantics: StringNumericValue, that is very informative. So the behavior is intended to match some formal standard - as I suspected but wasn't sure. (I also enjoyed you list of examples!) For a user on the consuming end of the data, if they want to correctly decode the pattern suffix to disambiguate the many-to-one mapping of strings to numbers, they must use of algorithm listed in Having a many-to-one mapping of keys strings to semantic keys, before using the keys to lookup the value in the object map seems anti intuitive and error prone. But the original problem lies in JS, not TS. On the other hand, if TS is going to impose some strict typing on the keys for arbitrary integers, why not go the whole way and allow specification of a one-to-one map? For an explicit limited range of integers it is already possible:
Maybe the feature request should be for a temple-key-string use specific intrinsic utility type "CountingIntegers" allowing all nonnegative integers up to |
Thanks to @jcalz pointing out 7.1.4.1.2 Runtime Semantics: StringNumericValue, it is now clear within a shadow of a doubt that the "surrounding space" behavior is "working as intended". So I will close this issue. |
Iβm not sureβ¦ Iβd really like to hear someone officially say that this is intentional. Iβm just an interested bystander. |
Same here regarding whitespace. |
This is very surprising to me and seems undesirable, but Iβll let @ahejlsberg have the definitive word. |
Yeah, we're basically just relying on JavaScript's intrinsic string-to-number conversion to validate I agree it makes more sense to not permit any whitespace and in particular to not permit empty strings. It's technically a breaking change, but can't imagine many types depend on it. |
And I'm still hung up on "finite"! To me, this behavior also seems strange: 1e999 satisfies number // Passes!
`${1e999}` satisfies `${number}` // Error: Type "Infinity" does not satisfy the expected type `${number}`
type Oops<N extends number, IsItReallyFinite extends `${number}` = `${N}`> = IsItReallyFinite
'Infinity' satisfies Oops<1e999> // Passes... I'd expect the correct definition to be "some non-empty, trimmed string that converts to a non-NaN number". Either that, or the type of |
Suggestion
π Search Terms
Template String Pattern Index Signatures
Template Index Strings
β Viability Checklist
My suggestion meets these guidelines:
Actually it would change the typescript behavior as it was released - maybe better now than later?
The current behavior might be more bug than feature.
β Suggestion
Not allowing spaces would reduce typing mistakes.
π Motivating Example
Modification of the example feature explanation in the handbook:
playground
π» Use Cases
Using it to catch unintended key entries.
The text was updated successfully, but these errors were encountered: