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();
}