@typescript-eslint/no-unnecessary-boolean-literal-compare

Disallow comparing a boolean with true or false

fixableneeds types

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.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-unnecessary-boolean-literal-compare" = "error"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-unnecessary-boolean-literal-compare" = ["error", {
  allowComparingNullableBooleansToTrue = true,
  allowComparingNullableBooleansToFalse = true,
  allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing = false,
}]

Examples#

example.ts2 problems
function canEdit(isOwner: boolean, isLocked: boolean) {
  return isOwner === true && isLocked === false;
}
example.tsNo problems
function canEdit(isOwner: boolean, isLocked: boolean) {
  return isOwner && !isLocked;
}

function isPinned(pinned?: boolean, mode?: boolean | string) {
  return pinned === true || mode === true;
}

Options#

OptionDefaultWhat it does
allowComparingNullableBooleansToTruetrueAllow value === true where value can also be null or undefined.
allowComparingNullableBooleansToFalsetrueAllow value === false where value can also be null or undefined.
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoingfalseWithout 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:

example.tswith { "allowComparingNullableBooleansToTrue": false }1 problem
function pin(isPinned?: boolean) {
  if (isPinned === true) {
    console.log("pinned");
  }
}

With allowComparingNullableBooleansToFalse off, write !(isPinned ?? true) instead, which says what undefined counts as:

example.tswith { "allowComparingNullableBooleansToFalse": false }1 problem
function pin(isPinned?: boolean) {
  if (isPinned === false) {
    console.log("not pinned");
  }
}