No Implicit Boolean
Require explicit comparisons in conditions instead of relying on truthy/falsy coercion.
Summary
The no-implicit-boolean rule reports bare identifiers, member expressions, call expressions, and their negations (!value, !fn()) when used as conditions in if, while, do...while, for, and ternary statements. For logical expressions (&&, ||), each operand is checked independently.
Why Use This Rule?
- Prevents subtle bugs from truthy/falsy coercion (e.g.,
0,"", andnullare all falsy). - Makes the developer's intent explicit (
=== undefined,=== true,> 0). - Keeps conditions self-documenting so readers understand what is actually being checked.
Examples
- Correct
- Incorrect
if (value !== undefined) {}
if (Array.isArray(x) === false) {}
if (a !== undefined && b === true) {}
while (items.length > 0) {}
// Ternary with explicit comparison — passes the check.
const label = (isDryRun === true) ? 'skip' : 'apply';
if (value) {}
if (!value) {}
while (items.length) {}
if (value && other) {}
Configuration
Enable the rule in your ESLint flat config:
import { noImplicitBoolean } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-implicit-boolean': noImplicitBoolean,
},
},
},
rules: {
'@cbnventures/nova/no-implicit-boolean': ['warn'],
},
},
];
This rule has no additional options.
Troubleshooting
- Warning fires on
!Array.isArray(x). — UseArray.isArray(x) === falseinstead. All negations of identifiers, member expressions, and call expressions are flagged. - Warning fires on
if (value && other). — Each operand in a logical expression is checked independently. Bothvalueandotherneed explicit comparisons. - Warning fires on a ternary condition. — Ternary conditions are checked the same as
ifconditions. Use an explicit comparison like(isDryRun === true) ? a : b.