I'm attempting to utilize tagged template literals from ES5 with TypeScript, but it appears that TypeScript doesn't fully support it. Here is the code snippet I have:
class TemplateLiterals {
age: number = 24;
name: 'Luke Skywalker'
private tag(strings: string[], personExp, ageExp) :string{
var str0 = strings[0]; // "that "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99) {
ageStr = 'centenarian';
} else {
ageStr = 'youngster';
}
return str0 + personExp + str1 + ageStr;
}
toString() {
return this.tag `that ${ this.name } is a ${ this.age }`;
}
}
In the toString
method, TypeScript displays the following error:
Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'string[]'.
Property 'push' is missing in type 'TemplateStringsArray'.
I am puzzled by why this error occurs. According to Mozilla's documentation, "The first argument of a tag function contains an array of string values." Therefore, it should accept the array of strings. However, there is an expectation of TemplateStringsArray
. I am currently using the TemplateStingArray
type to bypass this error. Can someone explain what's happening? Thanks. You can view the code on the playground.