Skip to content
← Back to rules

eslint/prefer-template Style

🛠️ An auto-fix is available for this rule.

作用

要求使用模板字面量,而不是字符串拼接。

为什么这不好?

在 ES2015(ES6)中,我们可以使用模板字面量来代替字符串拼接。

示例

以下是此规则的错误代码示例:

js
const str = "Hello, " + name + "!";
const str1 = "Time: " + 12 * 60 * 60 * 1000;

以下是此规则的正确代码示例:

js
const str = "Hello World!";
const str2 = `Time: ${12 * 60 * 60 * 1000}`;
const str4 = "Hello, " + "World!";

如何使用

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

json
{
  "rules": {
    "prefer-template": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "prefer-template": "error",
  },
});
bash
oxlint --deny prefer-template

版本

此规则于 v1.12.0 中添加。

参考资料