Skip to content
← Back to rules

eslint/no-void Restriction

💡 A suggestion 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

default: false

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

如何使用

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

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

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

版本

此规则在 v0.2.5 中新增。

参考