No Boolean Var For If
Inline the condition directly in the if-statement instead of extracting it into a single-use boolean variable.
Summary
The no-boolean-var-for-if rule reports a const declaration with a LogicalExpression initializer (e.g., &&, ||) when the variable is used exactly once as the test of the immediately following if-statement.
Variables used more than once or in a different context are not affected.
Why Use This Rule?
- A single-use boolean variable adds an indirection that makes the reader jump between two lines to understand the condition.
- Inlining the condition keeps the logic visible at the point of decision.
- Reduces unnecessary variable declarations that exist only to name a condition used once.
Examples
Configuration
Options
warning
ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | File patterns to skip. |
Autofix
Autofix is not available for this rule. Flagged code must be updated manually.
Troubleshooting
- Warning does not fire when the variable is used twice — The rule uses scope analysis and only flags variables that are read exactly once. If the variable is referenced in multiple places, it is not flagged.
- Warning does not fire on a non-logical initializer — Only
LogicalExpressioninitializers (&&,||,??) trigger the rule. Simple comparisons or function calls are not affected. - Warning fires on a
constwithlet— The rule only checksconstdeclarations.letdeclarations are not affected.