Skip to main content

no-unnecessary-boolean-literal-compare

Disallow unnecessary equality comparisons against boolean literals.

🔧

Some problems reported by this rule are automatically fixable by the --fix ESLint command line option.

💭

This rule requires type information to run.

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

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

Try this rule in the playground ↗

Examples

note

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

Options

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;
},
];

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

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 }:

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.

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

Fixer

ComparisonFixer OutputNotes
booleanVar === truebooleanVar
booleanVar !== true!booleanVar
booleanVar === false!booleanVar
booleanVar !== falsebooleanVar
nullableBooleanVar === truenullableBooleanVarOnly checked/fixed if the allowComparingNullableBooleansToTrue option is false
nullableBooleanVar !== true!nullableBooleanVarOnly checked/fixed if the allowComparingNullableBooleansToTrue option is false
nullableBooleanVar === false!(nullableBooleanVar ?? true)Only checked/fixed if the allowComparingNullableBooleansToFalse option is false
nullableBooleanVar !== falsenullableBooleanVar ?? trueOnly 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.

Resources