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 types starting with ${string} don't work when the string has more than 1 characterΒ #42903
constfoo: `${string}${number}` ='a123'//no errorconstbar: `${string}${number}` ='foo123'//Type '"foo123"' is not assignable to type '`${string}${number}`'.
π Actual behavior
Type '"foo123"' is not assignable to type '`${string}${number}`'
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[/+]on Feb 21, 2021
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[/+]on Feb 21, 2021
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.
Activity
[-]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[/+][-]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[/+]ahejlsberg commentedon Feb 23, 2021
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:
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.DetachHead commentedon Feb 24, 2021
i see. can this be changed to a feature suggestion instead of a bug then?
typescript-bot commentedon Feb 26, 2021
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.