@typescript-eslint/no-unnecessary-boolean-literal-compare
Disallow comparing a boolean with true or false
Also called typescript-eslint/no-unnecessary-boolean-literal-compare, typescript/no-unnecessary-boolean-literal-compare
If a value is a boolean, value === true is the same as value, and value === false is the same as !value. The comparison adds nothing.
A boolean | undefined is allowed by default, because there === true also decides what happens with undefined. A boolean | string is never reported.
--fix replaces the comparison with the value or its negation.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/no-unnecessary-boolean-literal-compare" = "error"Every option, set to its default:
[lint.rules]
"@typescript-eslint/no-unnecessary-boolean-literal-compare" = ["error", {
allowComparingNullableBooleansToTrue = true,
allowComparingNullableBooleansToFalse = true,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing = false,
}]Examples#
function canEdit(isOwner: boolean, isLocked: boolean) {
return isOwner === trueThis expression unnecessarily compares a boolean value to a boolean instead of using it directly. && isLocked === falseThis expression unnecessarily compares a boolean value to a boolean instead of using it directly.;
}function canEdit(isOwner: boolean, isLocked: boolean) {
return isOwner && !isLocked;
}
function isPinned(pinned?: boolean, mode?: boolean | string) {
return pinned === true || mode === true;
}Options#
| Option | Default | What it does |
|---|---|---|
allowComparingNullableBooleansToTrue | true | Allow value === true where value can also be null or undefined. |
allowComparingNullableBooleansToFalse | true | Allow value === false where value can also be null or undefined. |
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing | false | Without strictNullChecks in tsconfig.json, the rule can't tell what can be null, and it reports that once for each file. Set this to true to turn that report off. |
With allowComparingNullableBooleansToTrue off, write if (isPinned) instead:
{ "allowComparingNullableBooleansToTrue": false }1 problemfunction pin(isPinned?: boolean) {
if (isPinned === trueThis expression unnecessarily compares a nullable boolean value to true instead of using it directly.) {
console.log("pinned");
}
}With allowComparingNullableBooleansToFalse off, write !(isPinned ?? true) instead, which says what undefined counts as:
{ "allowComparingNullableBooleansToFalse": false }1 problemfunction pin(isPinned?: boolean) {
if (isPinned === falseThis expression unnecessarily compares a nullable boolean value to false instead of using the ?? operator to provide a default.) {
console.log("not pinned");
}
}