Rule: prefer-template
Prefer a template expression over string literal concatenation.
Config
If allow-single-concat
is specified, then a single concatenation (x + y
) is allowed, but not more (x + y + z
).
Config examples
"prefer-template": true
"prefer-template": [true, "allow-single-concat"]
Schema
{ "type": "string", "enum": [ "allow-single-concat" ] }
Code examples:
Enforces the use of template strings whenever possible.
"rules": { "prefer-template": true }
Passes
const x: number = 1;
const y: number = 1;
const myString: string = `${x} is equals ${y}`;
Fails
const x: number = 1;
const y: number = 1;
const myString: string = x + ' is equals ' + y;
Enforces the use of template strings, but allows up to one concatenation.
"rules": { "prefer-template": [true, "allow-single-concat"] }
Passes
const x: number = 1;
const y: number = 1;
const myString: string = x + ' is equals 1';
const myString: string = `${x} is equals ${y}`;
Fails
const x: number = 1;
const y: number = 1;
const myString: string = x + ' is equals ' + y;