no-unnecessary-template-expression
Disallow unnecessary template expressions.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked"
可启用此规则。
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
此规则报告何时可以用等效的 替换 调用。
¥This rule reports template literals that contain substitution expressions (also variously referred to as embedded expressions or string interpolations) that are unnecessary and can be simplified.
:::infono-useless-template-literals
][从以下对象迁移
此规则以前称为 no-useless-template-literals
。新名称是具有相同功能的替代品。
¥This rule was formerly known as no-useless-template-literals
.
The new name is a drop-in replacement with identical functionality.
:::
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-template-expression": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-template-expression": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
// Static values can be incorporated into the surrounding template.
const ab1 = `${'a'}${'b'}`;
const ab2 = `a${'b'}`;
type AB1 = `${'A'}${'B'}`;
type AB2 = `A${'B'}`;
const stringWithNumber = `${'1 + 1 = '}${2}`;
const stringWithBoolean = `${'true is '}${true}`;
// Some simple expressions that are already strings
// can be rewritten without a template at all.
const text = 'a';
const wrappedText = `${text}`;
type Text = 'A';
type WrappedText = `${Text}`;
declare const intersectionWithString: string & { _brand: 'test-brand' };
const wrappedIntersection = `${intersectionWithString}`;
type IntersectionWithString = string & { _brand: 'test-brand' };
type WrappedIntersection = `${IntersectionWithString}`;
Open in Playground// Static values can be incorporated into the surrounding template.
const ab1 = `ab`;
const ab2 = `ab`;
type AB = `AB`;
// Transforming enum members into string unions using template literals is allowed.
enum ABC {
A = 'A',
B = 'B',
C = 'C',
}
type ABCUnion = `${ABC}`;
type A = `${ABC.A}`;
// Interpolating type parameters is allowed.
type TextUtil<T extends string> = `${T}`;
const stringWithNumber = `1 + 1 = 2`;
const stringWithBoolean = `true is true`;
// Some simple expressions that are already strings
// can be rewritten without a template at all.
const text = 'a';
const wrappedText = text;
type Text = 'A';
type WrappedText = Text;
declare const intersectionWithString: string & { _brand: 'test-brand' };
const wrappedIntersection = intersectionWithString;
type IntersectionWithString = string & { _brand: 'test-brand' };
type WrappedIntersection = IntersectionWithString;
Open in Playground此规则的目的不是标记没有替换表达式的模板文字,这些模板文字可以写成普通字符串。也就是说,这条规则不会帮助你将 this
变成 "this"
。如果你正在寻找这样的规则,你可以配置 @stylistic/ts/quotes
规则来执行此操作。
¥This rule does not aim to flag template literals without substitution expressions that could have been written as an ordinary string.
That is to say, this rule will not help you turn `this`
into "this"
.
If you are looking for such a rule, you can configure the @stylistic/ts/quotes
rule to do this.
选项
该规则不可配置。
何时不使用它
¥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 Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.
'## 资源'