eslint/no-eq-null 限制
它做了什么
禁止在没有类型检查操作符的情况下与 null 进行比较。
为什么这是不好的?
在没有类型检查操作符(== 或 !=)的情况下与 null 进行比较,可能会产生意外的结果,因为该比较在与 null 以及 undefined 值进行比较时都会评估为 true。
示例
此规则的 错误 代码示例:
js
if (foo == null) {
bar();
}
if (baz != null) {
bar();
}此规则的 正确 代码示例:
js
if (foo === null) {
bar();
}
if (baz !== null) {
bar();
}
if (bang === undefined) {
bar();
}如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"rules": {
"no-eq-null": "error"
}
}bash
oxlint --deny no-eq-null