Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

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

Disallow comparing a boolean with true or false

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

Reported:

function canEdit(isOwner: boolean, isLocked: boolean) {
  return isOwner === true && isLocked === false;
}

Not reported:

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:

function pin(isPinned?: boolean) {
  if (isPinned === true) {
    console.log("pinned");
  }
}

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

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