Index

eqeqeq

要求使用 ===!==

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

💡 hasSuggestions

此规则报告的一些问题可通过编辑器 建议 手动修复

通常认为使用类型安全的相等运算符 ===!== 而不是它们的常规对应运算符 ==!= 是良好的做法。

🌐 It is considered good practice to use the type-safe equality operators === and !== instead of their regular counterparts == and !=.

原因是 ==!= 会进行类型强制转换,而这种转换遵循相当晦涩的 抽象相等比较算法。 例如,以下语句都被认为是 true

🌐 The reason for this is that == and != do type coercion which follows the rather obscure Abstract Equality Comparison Algorithm. For instance, the following statements are all considered true:

  • [] == false
  • [] == ![]
  • 3 == "03"

如果其中之一出现在像 a == b 这样看似无害的陈述中,实际问题就非常难以发现。

🌐 If one of those occurs in an innocent-looking statement such as a == b the actual problem is very difficult to spot.

规则详情

🌐 Rule Details

此规则旨在消除类型不安全的相等运算符。

🌐 This rule is aimed at eliminating the type-unsafe equality operators.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint eqeqeq: "error"*/

if (x == 42) { }

if ("" == text) { }

if (obj.getStuff() != undefined) { }

命令行上的 --fix 选项会自动修复此规则报告的一些问题。只有在其中一个操作数是 typeof 表达式,或者两个操作数都是相同类型的字面量时,一个问题才会被修复。

🌐 The --fix option on the command line automatically fixes some problems reported by this rule. A problem is only fixed if one of the operands is a typeof expression, or if both operands are literals with the same type.

选项

🌐 Options

always

"always" 选项(默认)在每种情况下都强制使用 ===!==(除非你选择对 null 进行更具体的处理 [见下文])。

🌐 The "always" option (default) enforces the use of === and !== in every situation (except when you opt-in to more specific handling of null [see below]).

针对 "always" 选项的错误代码示例:

🌐 Examples of incorrect code for the "always" option:

在线运行
/*eslint eqeqeq: ["error", "always"]*/

a == b
foo == true
bananas != 1
value == undefined
typeof foo == 'undefined'
'hello' != 'world'
0 == 0
true == true
foo == null

适用于 "always" 选项的正确代码示例:

🌐 Examples of correct code for the "always" option:

在线运行
/*eslint eqeqeq: ["error", "always"]*/

a === b
foo === true
bananas !== 1
value === undefined
typeof foo === 'undefined'
'hello' !== 'world'
0 === 0
true === true
foo === null

此规则可选地采用第二个参数,它应该是具有以下受支持属性的对象:

🌐 This rule optionally takes a second argument, which should be an object with the following supported properties:

  • "null":自定义此规则如何处理 null 字面量。可能的值:
    • always(default)- 始终使用 ===!==
    • never - 切勿将 ===!==null 一起使用。
    • ignore - 不要将此规则应用于 null

smart

"smart" 选项强制使用 ===!==,但以下情况除外:

🌐 The "smart" option enforces the use of === and !== except for these cases:

  • 比较两个字面量值。
  • 评估 typeof 的值。
  • null 进行比较。

针对 "smart" 选项的错误代码示例:

🌐 Examples of incorrect code for the "smart" option:

在线运行
/*eslint eqeqeq: ["error", "smart"]*/

// comparing two variables requires ===
a == b

// only one side is a literal
foo == true
bananas != 1

// comparing to undefined requires ===
value == undefined

适用于 "smart" 选项的正确代码示例:

🌐 Examples of correct code for the "smart" option:

在线运行
/*eslint eqeqeq: ["error", "smart"]*/

typeof foo == 'undefined'
'hello' != 'world'
0 == 0
true == true
foo == null

allow-null

已废弃: 不要使用此选项,请使用 "always" 并传递一个值为 "ignore""null" 选项属性。这将告诉 ESLint 始终强制执行严格相等,除非与 null 字面量进行比较。

["error", "always", {"null": "ignore"}]

何时不使用

🌐 When Not To Use It

如果你不想强制使用相等运算符的样式,那么禁用此规则是安全的。

🌐 If you don’t want to enforce a style for using equality operators, then it’s safe to disable this rule.

版本

此规则是在 ESLint v0.0.2 中引入。

资源