Do not allow surrounding spaces to be counted as part of ${number} templates in template index strings
See original GitHub issueSuggestion
🔍 Search Terms
Template String Pattern Index Signatures Template Index Strings
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
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:
interface Options {
width?: number;
height?: number;
}
let a: Options = {
width: 100,
height: 100,
"data-1": true, // Error! 'data-blah' wasn't declared in 'Options'.
};
interface OptionsWithDataProps extends Options {
// Permit any property matching 'data-${number}'.
[optName: `data-${number}`]: unknown;
}
let b: OptionsWithDataProps = {
width: 100,
height: 100,
"data-1": true, // Works!
"data- 1": true, // Passes but might be better off failing
"data-1 ": true, // Passes but might be better off failing
"data- 1 ": true, // Passes but might be better off failing
"data- ": true, // Passes but might be better off failing (no number at all)
"unknown-property": true, // Error! 'unknown-property' wasn't declared in 'OptionsWithDataProps'.
};
💻 Use Cases
Using it to catch unintended key entries.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:8 (6 by maintainers)
Top Results From Across the Web
type `${number}` cannot be used to index numeric ...
Do not allow surrounding spaces to be counted as part of ${number} templates in template index strings #46109.
Read more >Remove unnecessary whitespace from Jinja rendered ...
I'm using curl to watch the output of my web app. When Flask and Jinja render templates, there's a lot of unnecessary white...
Read more >Personalizing Templates with Handlebars
In an Iterable message template, you can add Handlebars expressions ... field that contains spaces or a period, or starts with a number,...
Read more >Language: Embedded Puppet (EPP) template syntax
See Templates for information about what templates are and how to evaluate them. EPP structure and syntax. <%- | Boolean $keys_enable, String $keys_file,...
Read more >Template Designer Documentation
It's important to know that the outer double-curly braces are not part of the ... String literals in templates with automatic escaping are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, we’re basically just relying on JavaScript’s intrinsic string-to-number conversion to validate
`${number}`
placeholder values–and it permits leading and trailing whitespace. Also, strangely, it permits empty strings and strings with only whitespace and returns0
for those.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.
This is very surprising to me and seems undesirable, but I’ll let @ahejlsberg have the definitive word.