no-unnecessary-boolean-literal-compare
Disallow unnecessary equality comparisons against boolean literals.
Extending "plugin:@typescript-eslint/strict-type-checked"
in an ESLint configuration enables this rule.
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
This rule requires type information to run, which comes with performance tradeoffs.
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.
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.).
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error"
}
};
Try this rule in the playground ↗
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.
- ❌ Incorrect
- ✅ Correct
declare const someCondition: boolean;
if (someCondition === true) {
}
Open in Playgrounddeclare const someCondition: boolean;
if (someCondition) {
}
declare const someObjectBoolean: boolean | Record<string, unknown>;
if (someObjectBoolean === true) {
}
declare const someStringBoolean: boolean | string;
if (someStringBoolean === true) {
}
Open in PlaygroundOptions
This rule accepts the following options:
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,
},
];
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
.
Examples of code for this rule with { allowComparingNullableBooleansToTrue: false }
:
- ❌ Incorrect
- ✅ Correct
declare const someUndefinedCondition: boolean | undefined;
if (someUndefinedCondition === true) {
}
declare const someNullCondition: boolean | null;
if (someNullCondition !== true) {
}
Open in Playgrounddeclare const someUndefinedCondition: boolean | undefined;
if (someUndefinedCondition) {
}
declare const someNullCondition: boolean | null;
if (!someNullCondition) {
}
Open in PlaygroundallowComparingNullableBooleansToFalse
Whether to allow comparisons between nullable boolean variables and false
. Default: true
.
Examples of code for this rule with { allowComparingNullableBooleansToFalse: false }
:
- ❌ Incorrect
- ✅ Correct
declare const someUndefinedCondition: boolean | undefined;
if (someUndefinedCondition === false) {
}
declare const someNullCondition: boolean | null;
if (someNullCondition !== false) {
}
Open in Playgrounddeclare const someUndefinedCondition: boolean | undefined;
if (!(someUndefinedCondition ?? true)) {
}
declare const someNullCondition: boolean | null;
if (someNullCondition ?? true) {
}
Open in PlaygroundallowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
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
.
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.
You should be using strictNullChecks
to ensure complete type-safety in your codebase.
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
Comparison | Fixer Output | Notes |
---|---|---|
booleanVar === true | booleanVar | |
booleanVar !== true | !booleanVar | |
booleanVar === false | !booleanVar | |
booleanVar !== false | booleanVar | |
nullableBooleanVar === true | nullableBooleanVar | Only checked/fixed if the allowComparingNullableBooleansToTrue option is false |
nullableBooleanVar !== true | !nullableBooleanVar | Only checked/fixed if the allowComparingNullableBooleansToTrue option is false |
nullableBooleanVar === false | !(nullableBooleanVar ?? true) | Only checked/fixed if the allowComparingNullableBooleansToFalse option is false |
nullableBooleanVar !== false | nullableBooleanVar ?? true | Only checked/fixed if the allowComparingNullableBooleansToFalse option is false |
When Not To Use It
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.