# no-extra-boolean-cast

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

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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

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

```toml 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.

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
if (!!user) {
  greet(user);
}

const label = Boolean(count) ? "some" : "none";
const isEmpty = !Boolean(items.length);
```

Not reported:

{/* good */}
```js
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:

{/* good */}
```js
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:

{/* good */}
```js
if (!!user || guest) {
  greet();
}
```

{/* bad {"options": [{"enforceForInnerExpressions": true}]} */}
```js
if (!!user || guest) {
  greet();
}
```

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

{/* bad {"options": [{"enforceForInnerExpressions": true}]} */}
```js
if (loggedIn ? !!user : guest) {
  greet();
}
```

{/* good {"options": [{"enforceForLogicalOperands": true}]} */}
```js
if (loggedIn ? !!user : guest) {
  greet();
}
```
