Skip to main content

no-unnecessary-boolean-literal-compare

Disallow unnecessary equality comparisons against boolean literals.

🔒

ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked" 可启用此规则。

🔧

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

💭

该规则需要 类型信息 才能运行。


没有必要将布尔值与布尔字面量进行比较: 这些比较会产生相同的布尔值。 直接使用布尔值或通过一元否定 (!value) 更简洁、更清晰。

英:Comparing boolean values to boolean literals is unnecessary: those comparisons result in the same booleans. Using the boolean values directly, or via a unary negation (!value), is more concise and clearer.

此规则可确保你不会包含与布尔字面量不必要的比较。 如果比较仅针对任何具有 boolean 类型的变量检查布尔字面量,则认为没有必要。 如果类型是布尔值的并集(string | booleanSomeObject | boolean 等),则 not 被认为是不必要的。

英:This rule ensures that you do not include unnecessary comparisons with boolean literals. A comparison is considered unnecessary if it checks a boolean literal against any variable with just the boolean type. A comparison is not considered unnecessary if the type is a union of booleans (string | boolean, SomeObject | boolean, etc.).

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error"
}
};
在线运行试试这个规则 ↗

示例

注意

在本页中,示例中仅使用严格相等(===!==)。 然而,该规则的实现并不区分严格平等和松散平等。 下面任何使用 === 的示例如果使用 ==,则将以相同的方式处理;如果使用 !=,则以相同的方式处理 !==

declare const someCondition: boolean;
if (someCondition === true) {
}
Open in Playground

选项

此规则始终检查布尔变量和布尔字面量之间的比较。 默认情况下,可空布尔变量和布尔字面量之间的比较是 not 检查。

英:This rule always checks comparisons between a boolean variable and a boolean literal. Comparisons between nullable boolean variables and boolean literals are not checked by default.

allowComparingNullableBooleansToTrue

此规则与 { allowComparingNullableBooleansToTrue: false } 的代码示例:

英:Examples of code for this rule with { allowComparingNullableBooleansToTrue: false }:

declare const someUndefinedCondition: boolean | undefined;
if (someUndefinedCondition === true) {
}

declare const someNullCondition: boolean | null;
if (someNullCondition !== true) {
}
Open in Playground

allowComparingNullableBooleansToFalse

此规则与 { allowComparingNullableBooleansToFalse: false } 的代码示例:

英:Examples of code for this rule with { allowComparingNullableBooleansToFalse: false }:

declare const someUndefinedCondition: boolean | undefined;
if (someUndefinedCondition === false) {
}

declare const someNullCondition: boolean | null;
if (someNullCondition !== false) {
}
Open in Playground

修复器

比较定影器输出注意
booleanVar === truebooleanVar
booleanVar !== true!booleanVar
booleanVar === false!booleanVar
booleanVar !== falsebooleanVar
nullableBooleanVar === truenullableBooleanVar仅当 allowComparingNullableBooleansToTrue 选项为 false 时才检查/修复
nullableBooleanVar !== true!nullableBooleanVar仅当 allowComparingNullableBooleansToTrue 选项为 false 时才检查/修复
!(nullableBooleanVar === false)nullableBooleanVar ?? true仅当 allowComparingNullableBooleansToFalse 选项为 false 时才检查/修复
!(nullableBooleanVar !== false)!(nullableBooleanVar ?? true)仅当 allowComparingNullableBooleansToFalse 选项为 false 时才检查/修复

何时不使用它

strictNullChecks 被禁用时,请勿使用此规则。 ESLint 无法区分 falseundefinednull 值。 使用自动修复时,这可能会导致意外的代码更改。

英:Do not use this rule when strictNullChecks is disabled. ESLint is not able to distinguish between false and undefined or null values. This can cause unintended code changes when using autofix.

选项

该规则接受以下选项

type Options = [
{
/** Whether to allow comparisons between nullable boolean variables and `false`. */
allowComparingNullableBooleansToFalse?: boolean;
/** Whether to allow comparisons between nullable boolean variables and `true`. */
allowComparingNullableBooleansToTrue?: boolean;
},
];

const defaultOptions: Options = [
{
allowComparingNullableBooleansToTrue: true,
allowComparingNullableBooleansToFalse: true,
},
];

资源