You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Template literal type ${string}${number}${string} errors on valid values #57355
constmyString:`${string}${number}${string}` ='test123test';// ^^^^^^^^ Type '"test123test"' is not assignable to type '`${string}${number}${string}`'.ts(2322)constmyString2:`${string}${number}` ='test123';// ^^^^^^^^ Type '"test123"' is not assignable to type '`${string}${number}`'.ts(2322)constmyString3:`${number}${string}` ='123test';// passes for some reason
🙁 Actual behavior
Received error "Type '"test123test"' is not assignable to type '${string}${number}${string}'.ts(2322)" when pattern matches
🙂 Expected behavior
I expect it to match any literal string type that has a number within it
Additional information about the issue
My real use case is that I would like to be able to exclude/omit patterns like ${string}${number}${string}, but that's not possible since it doesn't match
See #57126 and essentially intentional as described in #40336:
A placeholder immediately followed by another placeholder is matched by inferring a single character from the source.
So `${string}${number}${string}` matches the first character against string, the second against number, and then the rest of the string against string. Yes, it's weird.
As a general principle, template string literals aren't regexes and don't have regex capabilities. Anything that requires backtracking (or extensive lookahead) isn't going to happen.
Activity
jcalz commentedon Feb 9, 2024
See #57126 and essentially intentional as described in #40336:
So
`${string}${number}${string}`matches the first character againststring, the second againstnumber, and then the rest of the string againststring. Yes, it's weird.RyanCavanaugh commentedon Feb 9, 2024
As a general principle, template string literals aren't regexes and don't have regex capabilities. Anything that requires backtracking (or extensive lookahead) isn't going to happen.