-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Transpiled template literals behave incorrectly #39744
Copy link
Copy link
Closed
Labels
Fix AvailableA PR has been opened for this issueA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Metadata
Metadata
Assignees
Labels
Fix AvailableA PR has been opened for this issueA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Type
Fields
Give feedbackNo fields configured for issues without a type.
TypeScript Version: 3.9.7, 4.0.0-dev.20200725
Search Terms:
template literals, template strings, ES5, ES3
Code
Expected behavior:
According to ES2015 (and later) spec, the result of expressions in template literals are converted to primitives first by calling
toString()and then byvalueOf()(unless there is[Symbol.toPrimitive](), but I'll just ignore it here asSymbols wouldn't get transpiled).stherefore evaluates to"toString()".Actual behavior:
sevaluates to"valueOf()".The above code is compiled to the following when the target is ES5 or ES3.
+operator attempts to convert its operands to primitives first by callingvalueOf()and then bytoString(), which is the opposite of the expected behavior.FWIW, Babel compiles the above code to this unless loose transformation is enabled. It makes use of
String.prototype.concat()and behaves as expected.Playground Link: Link
Related Issues:
#30239: Although OP's suggestion is not related, some people commenting on the issue seem to confront this issue (like this comment)