no-useless-template-literals
Disallow unnecessary template literals.
🔒
Extending "plugin:@typescript-eslint/strict-type-checked"
in an ESLint configuration enables this rule.
🔧
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
💭
This rule requires type information to run.
This rule reports template literals that can be simplified to a normal string literal.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-useless-template-literals": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
const ab1 = `${'a'}${'b'}`;
const ab2 = `a${'b'}`;
const stringWithNumber = `${'1 + 1 = '}${2}`;
const stringWithBoolean = `${'true is '}${true}`;
const text = 'a';
const wrappedText = `${text}`;
declare const intersectionWithString: string & { _brand: 'test-brand' };
const wrappedIntersection = `${intersectionWithString}`;
Open in Playgroundconst ab1 = 'ab';
const ab2 = 'ab';
const stringWithNumber = `1 + 1 = 2`;
const stringWithBoolean = `true is true`;
const text = 'a';
const wrappedText = text;
declare const intersectionWithString: string & { _brand: 'test-brand' };
const wrappedIntersection = intersectionWithString;
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
When you want to allow string expressions inside template literals.
Related To
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting. See Performance Troubleshooting if you experience performance degredations after enabling type checked rules.