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
Treat + on string literal types the same way as string templatesΒ #44905
typeLetter='A'|'B';typeNum='1'|'2';typeLabel= `${Letter}${Num}`;letletter: Letter='A';// type='A'letnum: Num='1';// type='1'letlabel1: Label=`${letter}${num}`;// does work, type="A1" | "A2" | "B1" | "B2"letlabel2_inferred=letter+num// type = string (!!)letlabel2: Label=letter+num;// doesn't work
π Actual behavior
label2 gives Error: Type 'string' is not assignable to type '"A1" | "A2" | "B1" | "B2"'.
It should not be a string type as both Letter and Num are clearly defined values.
π Expected behavior
label2 and label1 should be identical.
Template literals and string concatenation should act identical when joining strings.
π Version & Regression Information
This changed between versions 4.2.3 and 4.3.5
Both label1 and label2 are invalid in 4.2, only label2 is in 4.3 / 4.4alpha.
changed the title [-]Template literal values are treated differently from string concatenation[/-][+]Treat `+` on string literal types the same way as string templates[/+]on Jul 6, 2021
We've discussed this before and, as I recall, concluded support for + on literal types would have to also extend to numeric literal types. Which in turn would bring -, *, / and possibly other operators into play. It wasn't clear where we'd stop. Another issue that concerned us is the quadratic nature of resolving the type operators applied to unions. For example, + on two unions each of 100 literal types produces a union of as many as 10,000 literal types. This is true of template literals as well of course, but those don't look quite as innocent.
Maybe add a note to the error message then (if new type is string)? "If the more specific type is required, use a template string instead" with a quick fix attached.
Activity
[-]Template literal values are treated differently from string concatenation[/-][+]Treat `+` on string literal types the same way as string templates[/+]ahejlsberg commentedon Jul 7, 2021
We've discussed this before and, as I recall, concluded support for
+on literal types would have to also extend to numeric literal types. Which in turn would bring-,*,/and possibly other operators into play. It wasn't clear where we'd stop. Another issue that concerned us is the quadratic nature of resolving the type operators applied to unions. For example,+on two unions each of 100 literal types produces a union of as many as 10,000 literal types. This is true of template literals as well of course, but those don't look quite as innocent.Nixinova commentedon Jul 7, 2021
Maybe add a note to the error message then (if new type is string)? "If the more specific type is required, use a template string instead" with a quick fix attached.
Nixinova commentedon Jul 22, 2021
Anyway closing per ahejlsberg's comment