Skip to content
← Back to rules

eslint/no-unused-expressions 正确性

This rule is turned on by default.

它的作用

此规则禁止未使用的表达式。

为什么这不好?

未使用的表达式通常是一个错误。它们可能是 bug 的症状,或者是对代码的误解。

示例

此规则的错误代码示例:

ts
Set<number>;
1 as number;
window!;

此规则的正确代码示例:

ts
const foo = new Set<number>();

配置

此规则接受一个包含以下属性的配置对象:

allowShortCircuit

type: boolean

default: false

设置为 true 时,允许表达式中的短路求值。

allowTaggedTemplates

type: boolean

default: false

设置为 true 时,允许表达式中的带标签模板字面量。

allowTernary

type: boolean

default: false

设置为 true 时,允许表达式中的三元运算符。

enforceForJSX

type: boolean

default: false

设置为 true 时,也会对未使用的 JSX 表达式强制执行此规则。

如何使用

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

json
{
  "rules": {
    "no-unused-expressions": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-unused-expressions": "error",
  },
});
bash
oxlint --deny no-unused-expressions

版本

此规则在 v0.14.0 中添加。

参考资料