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 等),则不认为比较是不必要的。

¥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.).

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

注意

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

¥Throughout this page, only strict equality (=== and !==) are used in the examples. However, the implementation of the rule does not distinguish between strict and loose equality. Any example below that uses === would be treated the same way if == was used, and !== would be treated the same way if != was used.

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

选项

该规则接受以下选项:

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;
/** Unless this is set to `true`, the rule will error on every file whose `tsconfig.json` does _not_ have the `strictNullChecks` compiler option (or `strict`) set to `true`. */
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
},
];

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

¥Options

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

¥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

Whether to allow comparisons between nullable boolean variables and true. Default: true.

带有 { 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

Whether to allow comparisons between nullable boolean variables and false. Default: true.

带有 { 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

allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing

已弃用

此选项将在下一个主要版本的 typescript-eslint 中删除。

¥This option will be removed in the next major version of typescript-eslint.

Unless this is set to true, the rule will error on every file whose tsconfig.json does not have the strictNullChecks compiler option (or strict) set to true. Default: false.

如果没有 strictNullChecks,TypeScript 基本上会从类型中删除 undefinednull。这意味着当此规则检查变量的类型时,它将无法判断变量可能是 null 还是 undefined,这实际上使此规则毫无用处。

¥Without strictNullChecks, TypeScript essentially erases undefined and null from the types. This means when this rule inspects the types from a variable, it will not be able to tell that the variable might be null or undefined, which essentially makes this rule useless.

你应该使用 strictNullChecks 来确保代码库中的完全类型安全。

¥You should be using strictNullChecks to ensure complete type-safety in your codebase.

如果由于某种原因你无法打开 strictNullChecks,但仍想使用此规则 - 你可以使用此选项来允许它 - 但要知道,如果关闭编译器选项,此规则的行为是未定义的。如果你使用此选项,我们将不接受错误报告。

¥If for some reason you cannot turn on strictNullChecks, but still want to use this rule - you can use this option to allow it - but know that the behavior of this rule is undefined with the compiler option turned off. We will not accept bug reports if you are using this option.

修复器

¥Fixer

比较定影器输出注意
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 !== falsenullableBooleanVar ?? true仅当 allowComparingNullableBooleansToFalse 选项为 false 时才检查/修复

何时不使用它

¥When Not To Use It

禁用 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 checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'