question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not allow surrounding spaces to be counted as part of ${number} templates in template index strings

See original GitHub issue

Suggestion

🔍 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'.
};

playground

💻 Use Cases

Using it to catch unintended key entries.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
ahejlsbergcommented, Sep 29, 2021

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 returns 0 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.

2reactions
andrewbranchcommented, Sep 28, 2021

This is very surprising to me and seems undesirable, but I’ll let @ahejlsberg have the definitive word.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found