no-extra-boolean-cast

Disallow !! and Boolean() where the value is converted to a boolean anyway

warn by defaultfixable

The test of an if, a loop or a ? :, the operand of !, and the argument of Boolean() are already converted to a boolean. Writing !!value or Boolean(value) there converts twice and gives the same result.

--fix removes the cast.

This rule is on by default.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"no-extra-boolean-cast" = "off" # or "error"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"no-extra-boolean-cast" = ["warn", {
  enforceForInnerExpressions = false,
  # enforceForLogicalOperands = false,
}]

Examples#

example.js3 problems
if (!!user) {
  greet(user);
}

const label = Boolean(count) ? "some" : "none";
const isEmpty = !Boolean(items.length);
example.jsNo problems
if (user) {
  greet(user);
}

const label = count ? "some" : "none";
const isEmpty = !items.length;

When you store or return the value, the cast does something, so it's allowed:

example.jsNo problems
const isActive = !!user.active;
const hasItems = Boolean(items.length);

Options#

OptionDefaultWhat it does
enforceForInnerExpressionsfalseAlso look inside ||, &&, ??, ? : and comma expressions that are used as a test.
enforceForLogicalOperandsfalseThe same, for || and && only.

Set one or the other. A config with both is rejected.

By default only the outermost expression of a test is checked:

example.jsNo problems
if (!!user || guest) {
  greet();
}
example.jswith { "enforceForInnerExpressions": true }1 problem
if (!!user || guest) {
  greet();
}

enforceForLogicalOperands reports that one too. The two differ on the other operators:

example.jswith { "enforceForInnerExpressions": true }1 problem
if (loggedIn ? !!user : guest) {
  greet();
}
example.jswith { "enforceForLogicalOperands": true }No problems
if (loggedIn ? !!user : guest) {
  greet();
}