Skip to content

noDoubleEquals

¥Summary

  • 规则生效日期:v1.0.0

    ¥Rule available since: v1.0.0

  • 诊断类别:lint/suspicious/noDoubleEquals

    ¥Diagnostic Category: lint/suspicious/noDoubleEquals

  • 此规则为推荐规则,默认启用。

    ¥This rule is recommended, which means is enabled by default.

  • 此规则包含 unsafe 修复程序。

    ¥This rule has an unsafe fix.

  • 此规则的默认严重级别为 error

    ¥The default severity of this rule is error.

  • 来源:

    ¥Sources:

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noDoubleEquals": "error"
}
}
}
}

¥Description

需要使用 ===!==

¥Require the use of === and !==.

使用 == 而不是 === 进行比较通常是不好的做法。双运算符将触发隐式 类型强制,因此不推荐。使用严格相等运算符几乎总是最佳实践。

¥It is generally bad practice to use == for comparison instead of ===. Double operators will trigger implicit type coercion and are thus not preferred. Using strict equality operators is almost always best practice.

出于人机工程学原因,此规则默认将 == nullnullundefined 进行比较。

¥For ergonomic reasons, this rule makes by default an exception for == null for comparing to both null and undefined.

¥Examples

¥Invalid

foo == bar
code-block.js:1:5 lint/suspicious/noDoubleEquals  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Using == may be unsafe if you are relying on type coercion.

> 1 │ foo == bar
^^
2 │

== is only allowed when comparing against null.

Unsafe fix: Use === instead.

1 │ foo·===·bar
+

¥Valid

foo == null
foo != null
null == foo
null != foo

¥Options

规则提供下面描述的选项。

¥The rule provides the option described below.

{
"//":"...",
"options": {
"ignoreNull": true
}
}

当此选项设置为 true 时,将对 null 进行检查,因为依赖双等号运算符与 null 进行比较通常用于检查与 nullundefined 的相等性。

¥When this option is set to true, an exception will be made for checking against null, as relying on the double equals operator to compare with null is frequently used to check equality with either null or undefined.

当选项设置为 false 时,所有双等号运算符都将被禁止,无一例外。

¥When the option is set to false, all double equal operators will be forbidden without exceptions.

默认:true

¥Default: true

¥Related links