eslint/no-eq-null Restriction
作用
禁止在不使用类型检查运算符的情况下进行 null 比较。
为什么这不好?
在没有类型检查运算符(== 或 !=)的情况下与 null 比较, 可能会产生意外结果,因为这类比较不仅在与 null 比较时会计算为 true, 在与 undefined 值比较时也会如此。
示例
以下是此规则的错误代码示例:
js
if (foo == null) {
bar();
}
if (baz != null) {
bar();
}以下是此规则的正确代码示例:
js
if (foo === null) {
bar();
}
if (baz !== null) {
bar();
}
if (bang === undefined) {
bar();
}如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-eq-null": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-eq-null": "error",
},
});bash
oxlint --deny no-eq-null版本
此规则是在 v0.2.14 中添加的。
