useStrictBooleanExpressions (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.5.15 - Diagnostic Category:
lint/nursery/useStrictBooleanExpressions - This rule doesn’t have a fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
@typescript-eslint/strict-boolean-expressions
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useStrictBooleanExpressions": "error" } } }}Description
Section titled “Description”Require unambiguous boolean expressions in conditions.
Truthiness checks on nullable primitives can confuse missing values with
false, an empty string, zero, or NaN. Check for nullish values explicitly
or convert the value with Boolean() when that distinction is intentional.
This rule allows booleans, non-nullable strings and numbers, and nullable
objects, functions, and symbols. Nullable true, nonempty string literal
types, and nonzero number literal types are also allowed. Numbers include
bigints, but nullable bigints are rejected.
It checks conditions, logical negation, the operands of && and ||, and
array predicate callbacks and truthiness assertion arguments. The last operand of a logical expression is
checked only when its result is used as a condition.
Types that cannot be inferred are ignored.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function display(count: number | undefined) { if (count) {}}/nullable-number.ts:2:9 lint/nursery/useStrictBooleanExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This condition uses a nullable number.
1 │ function display(count: number | undefined) {
> 2 │ if (count) {}
│ ^^^^^
3 │ }
4 │
ℹ This check treats zero, NaN, and nullish values alike.
ℹ Check for nullish values and falsy numbers explicitly, or use Boolean() to test truthiness.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function run(enabled?: boolean) { if (enabled) {}}/nullable-boolean.ts:2:9 lint/nursery/useStrictBooleanExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This condition uses a nullable boolean.
1 │ function run(enabled?: boolean) {
> 2 │ if (enabled) {}
│ ^^^^^^^
3 │ }
4 │
ℹ This check treats false and nullish values alike.
ℹ Compare with true or provide an explicit default with ?? false.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
if ({}) {}/object-condition.js:1:5 lint/nursery/useStrictBooleanExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This condition uses an always-truthy value.
> 1 │ if ({}) {}
│ ^^
2 │
ℹ Objects, functions, and symbols are always truthy.
ℹ Check a property of the value or remove the condition.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function run(maybeCount: number | undefined, count: number, text: string, object: object | null, enabled?: boolean) { if (maybeCount != null) {} if (enabled === true) {} if (text) {} if (count) {} if (object) {}}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.