Skip to content
← Back to rules

eslint/no-void 限制

An auto-fix is available for this rule.

它的作用

禁止使用 void 操作符。

为什么这是个问题?

void 操作符通常用于获取 undefined,但这种用法是不必要的,因为可以直接使用 undefined

示例

此规则的错误代码示例:

ts
void 0;
var foo = void 0;

此规则的正确代码示例:

ts
"var foo = bar()";
"foo.void()";
"foo.void = bar";

配置

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

allowAsStatement

type: boolean

默认值: false

如果设置为 true,则允许将 void 作为独立语句使用。

如何使用

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

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

参考资料