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 (!!userRedundant double negation.) {
greet(user);
}
const label = Boolean(count)Redundant Boolean call. ? "some" : "none";
const isEmpty = !Boolean(items.length)Redundant Boolean call.;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#
| Option | Default | What it does |
|---|---|---|
enforceForInnerExpressions | false | Also look inside ||, &&, ??, ? : and comma expressions that are used as a test. |
enforceForLogicalOperands | false | The 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 problemif (!!userRedundant double negation. || guest) {
greet();
}enforceForLogicalOperands reports that one too. The two differ on the other operators:
example.jswith
{ "enforceForInnerExpressions": true }1 problemif (loggedIn ? !!userRedundant double negation. : guest) {
greet();
}example.jswith
{ "enforceForLogicalOperands": true }No problemsif (loggedIn ? !!user : guest) {
greet();
}