Skip to content

template literal types starting with ${string} don't work when the string has more than 1 character #42903

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

Closed
DetachHead opened this issue Feb 21, 2021 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Feb 21, 2021

Bug Report Suggestion

πŸ”Ž Search Terms

template literal types string

πŸ•— Version & Regression Information

4.3.0-dev.20210221

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about trmplate literal types

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.3.0-dev.20210221#code/MYewdgzgLgBAZiEAuGADAJAb2gJwJZgDmAvlmAK4C2ARgKY7GowC8MA5AIYCMATAMxsYAeiFgQMejhA4AsAChQkWNQ44UGbFHxFSmCjXqMW7BCF4DhQgCoBPAA612AIlPmngvBBhjYHCBDxCMA5qABtHKHEoe0c2DVwCEjIqOgZUNgA6IA

πŸ’» Code

const foo: `${string}${number}` = 'a123' //no error
const bar: `${string}${number}` = 'foo123' //Type '"foo123"' is not assignable to type '`${string}${number}`'.

πŸ™ Actual behavior

Type '"foo123"' is not assignable to type '`${string}${number}`'

πŸ™‚ Expected behavior

no error

@DetachHead DetachHead changed the title template literal types starting with ${string} don't work template literal types starting with ${string} don't work when the string is more than 1 character Feb 21, 2021
@DetachHead DetachHead changed the title template literal types starting with ${string} don't work when the string is more than 1 character template literal types starting with ${string} don't work when the string has more than 1 character Feb 21, 2021
@ahejlsberg
Copy link
Member

This is working as intended. The pattern matching we use in template literal type inference and assignability checking is (by design) pretty simple. From #40336:

Type inference supports inferring from a string literal type to a template literal type. For inference to succeed the starting and ending literal character spans (if any) of the target must exactly match the starting and ending spans of the source. Inference proceeds by matching each placeholder to a substring in the source from left to right: A placeholder followed by a literal character span is matched by inferring zero or more characters from the source until the first occurrence of that literal character span in the source. A placeholder immediately followed by another placeholder is matched by inferring a single character from the source.

For your example code to behave as you expected we'd have to scan ahead and find the point where the remainder of the string is a valid number. But valid numbers include things like 1e1, Nan, Infinity, and so on. So, we simply don't attempt that and instead always match exactly one character for a placeholder immediately followed by another placeholder.

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 23, 2021
@DetachHead
Copy link
Contributor Author

i see. can this be changed to a feature suggestion instead of a bug then?

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants