Skip to content

noUnnecessaryTemplateExpression (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUnnecessaryTemplateExpression": "error"
}
}
}
}

Disallow unnecessary template expressions.

A template expression (or template literal) is unnecessary when it only contains string literal expressions that could be written as a regular string literal instead.

const a = `${'hello'}`;
code-block.js:1:11 lint/nursery/noUnnecessaryTemplateExpression  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This template expression is unnecessary.

> 1 │ const a = `${'hello'}`;
│ ^^^^^^^^^^^^
2 │

ℹ The template only contains string literal expressions. A regular string literal can be used instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Replace the template expression with a string literal.

1 │ - const·a·=·`${'hello'}`;
1 │ + const·a·=·"hello";
2 2 │

const b = `${"world"}`;
code-block.js:1:11 lint/nursery/noUnnecessaryTemplateExpression  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This template expression is unnecessary.

> 1 │ const b = `${"world"}`;
│ ^^^^^^^^^^^^
2 │

ℹ The template only contains string literal expressions. A regular string literal can be used instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Replace the template expression with a string literal.

1 │ const·b·=·`${"world"}`;
│ --- --
const c = `${'hello'}${'world'}`;
code-block.js:1:11 lint/nursery/noUnnecessaryTemplateExpression  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This template expression is unnecessary.

> 1 │ const c = `${'hello'}${'world'}`;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The template only contains string literal expressions. A regular string literal can be used instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Replace the template expression with a string literal.

1 │ - const·c·=·`${'hello'}${'world'}`;
1 │ + const·c·=·"helloworld";
2 2 │

const d = `prefix_${'suffix'}`;
code-block.js:1:11 lint/nursery/noUnnecessaryTemplateExpression  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This template expression is unnecessary.

> 1 │ const d = `prefix_${'suffix'}`;
│ ^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The template only contains string literal expressions. A regular string literal can be used instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Replace the template expression with a string literal.

1 │ - const·d·=·`prefix_${'suffix'}`;
1 │ + const·d·=·"prefix_suffix";
2 2 │

// Template with a non-string-literal expression
const a = `${someVariable}`;
// Template with a non-string-literal interpolation mixed with text
const b = `Hello, ${name}!`;
// Tagged templates are never flagged
const c = html`${'foo'}`;
// Templates with newlines in the text part need the template syntax
const d = `line one
${'line two'}`;