Skip to content

typescript/no-unnecessary-template-expression Suspicious

🚧 An auto-fix is still under development.

What it does

This rule disallows unnecessary template literals.

Why is this bad?

Template literals should only be used when they are needed for string interpolation or multi-line strings. Using template literals when a simple string would suffice adds unnecessary complexity.

Examples

Examples of incorrect code for this rule:

ts
const str1 = `Hello world`;

const str2 = `42`;

const str3 = `true`;

// Template with only literal expressions
const str4 = `${"Hello"} ${"world"}`;

Examples of correct code for this rule:

ts
const str1 = "Hello world";

const str2 = "42";

const str3 = "true";

// Template with variable interpolation
const name = "world";
const str4 = `Hello ${name}`;

// Multi-line string
const multiline = `
  Hello
  world
`;

// Template with expression
const str5 = `Result: ${1 + 2}`;

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny typescript/no-unnecessary-template-expression
json
{
  "rules": {
    "typescript/no-unnecessary-template-expression": "error"
  }
}

References

Released under the MIT License.