Skip to content
← Back to rules

eslint/no-unused-expressions 正确性

This rule is turned on by default.

它做了什么

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

为什么这是问题?

未使用的表达式通常是错误。它们可能是代码中存在缺陷或对代码理解错误的迹象。

示例

此规则的错误代码示例:

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 表达式强制执行该规则。

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "no-unused-expressions": "error"
  }
}
bash
oxlint --deny no-unused-expressions

参考资料