useNullishCoalescing
Summary
Section titled “Summary”- Rule available since:
v2.4.5 - Diagnostic Category:
lint/nursery/useNullishCoalescing - This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
@typescript-eslint/prefer-nullish-coalescing
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useNullishCoalescing": "error" } } }}Description
Section titled “Description”Enforce using the nullish coalescing operator (??) instead of logical or (||).
The ?? operator only checks for null and undefined, while || checks
for any falsy value including 0, '', and false. This can prevent bugs
where legitimate falsy values are incorrectly treated as missing.
For || expressions, this rule triggers when the left operand is possibly
nullish (contains null or undefined in its type). A safe fix is only
offered when type analysis confirms the left operand can only be truthy or
nullish (not other falsy values like 0 or '').
For ||= assignment expressions, the same logic applies: a ||= b is
flagged when a is possibly nullish and can be rewritten as a ??= b.
For ternary expressions, this rule detects patterns like x !== null ? x : y
and suggests rewriting them as x ?? y. This applies to strict and loose
equality checks against null or undefined, including compound checks.
By default, || expressions in conditional test positions (if/while/for/ternary)
are ignored, as the falsy-checking behavior is often intentional there. This can
be disabled with the ignoreConditionalTests option.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”declare const maybeString: string | null;const value = maybeString || 'default'; // should use ??declare const maybeNumber: number | undefined;const value = maybeNumber || 0; // should use ??declare let x: string | null;x ||= 'default'; // should use ??=declare const x: string | null;const value = x !== null ? x : 'default'; // should use ??code-block.ts:2:15 lint/nursery/useNullishCoalescing FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer ?? over a ternary expression checking for nullish.
1 │ declare const x: string | null;
> 2 │ const value = x !== null ? x : ‘default’; // should use ??
│ ^^^^^^^^^^
3 │
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.
ℹ 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.
ℹ Safe fix: Use ?? instead.
1 1 │ declare const x: string | null;
2 │ - const·value·=·x·!==·null·?·x·:·‘default’;·//·should·use·??
2 │ + const·value·=·x·??·‘default’;·//·should·use·??
3 3 │
declare const x: string | null;const value = x == null ? 'default' : x; // should use ??code-block.ts:2:15 lint/nursery/useNullishCoalescing FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer ?? over a ternary expression checking for nullish.
1 │ declare const x: string | null;
> 2 │ const value = x == null ? ‘default’ : x; // should use ??
│ ^^^^^^^^^
3 │
ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.
ℹ 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.
ℹ Safe fix: Use ?? instead.
1 1 │ declare const x: string | null;
2 │ - const·value·=·x·==·null·?·‘default’·:·x;·//·should·use·??
2 │ + const·value·=·x·??·‘default’;·//·should·use·??
3 3 │
// Already using ??declare const maybeString: string | null;const value = maybeString ?? 'default';// Type is not nullish - no null or undefined in uniondeclare const definiteString: string;const value = definiteString || 'fallback';// In conditional test position (ignored by default)declare const cond: string | null;if (cond || 'fallback') { console.log('in if');}// Already using ??=declare let y: string | null;y ??= 'default';Options
Section titled “Options”ignoreConditionalTests
Section titled “ignoreConditionalTests”Ignore || expressions inside conditional test positions
(if/while/for/do-while/ternary conditions), where falsy-checking
behavior may be intentional.
Default: true
Setting this to false will report || in conditional tests:
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "options": { "ignoreConditionalTests": false } } } } }}declare const cond: string | null;if (cond || 'fallback') {}ignoreTernaryTests
Section titled “ignoreTernaryTests”Ignore ternary expressions that check for null or undefined
and could be replaced with ??.
Default: false
Setting this to true will suppress ternary diagnostics:
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "options": { "ignoreTernaryTests": true } } } } }}declare const x: string | null;const value = x !== null ? x : 'default'; // no diagnosticRelated links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.