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 (||).
?? only checks for null and undefined, while || checks for any falsy value
including 0, '', and false. The rule reports ||, ||=, and ternary patterns
(x !== null ? x : y) when type analysis shows the left operand is possibly nullish.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”declare const maybeString: string | null;const value = maybeString || 'default';/invalid-or.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || ‘default’;
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
declare const maybeNumber: number | undefined;const value = maybeNumber || 0;/invalid-or-undefined.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const maybeNumber: number | undefined;
> 2 │ const value = maybeNumber || 0;
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
declare let x: string | null;x ||= 'default';/invalid-or-assign.ts:2:3 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ??= instead of ||=.
1 │ declare let x: string | null;
> 2 │ x ||= ‘default’;
│ ^^^
3 │
ℹ The ||= operator assigns when the left side is falsy, while ??= only assigns when it is null or undefined.
ℹ 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.
declare const x: string | null;const value = x !== null ? x : 'default';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’;
│ ^^^^^^^^^^
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: Replace the ternary with ??.
1 1 │ declare const x: string | null;
2 │ - const·value·=·x·!==·null·?·x·:·‘default’;
2 │ + const·value·=·x·??·‘default’;
3 3 │
declare const x: string | null;const value = x == null ? 'default' : x;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;
│ ^^^^^^^^^
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: Replace the ternary with ??.
1 1 │ declare const x: string | null;
2 │ - const·value·=·x·==·null·?·‘default’·:·x;
2 │ + const·value·=·x·??·‘default’;
3 3 │
An if statement that only assigns to a nullish variable is also reported,
since it can be rewritten as ??=.
declare let a: { x: string } | null;declare function makeA(): { x: string };if (!a) { a = makeA();}/invalid-if-assignment.ts:3:1 lint/nursery/useNullishCoalescing FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ??= instead of an if statement for nullish assignment.
1 │ declare let a: { x: string } | null;
2 │ declare function makeA(): { x: string };
> 3 │ if (!a) {
│ ^^^^^^^^^
> 4 │ a = makeA();
> 5 │ }
│ ^
6 │
ℹ This if statement only assigns when the variable is nullish, which ??= expresses directly.
ℹ 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: Replace the if statement with ??=.
1 1 │ declare let a: { x: string } | null;
2 2 │ declare function makeA(): { x: string };
3 │ - if·(!a)·{
4 │ - ····a·=·makeA();
5 │ - }
3 │ + a·??=·makeA();
6 4 │
declare const maybeString: string | null;const value = maybeString ?? 'default';declare const definiteString: string;const value = definiteString || 'fallback';declare const cond: string | null;if (cond || 'fallback') { console.log('in if');}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).
Default: true.
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "level": "on", "options": { "ignoreConditionalTests": false } } } } }}declare const cond: string | null;if (cond || 'fallback') {}code-block.ts:2:10 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const cond: string | null;
> 2 │ if (cond || ‘fallback’) {}
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
ignoreTernaryTests
Section titled “ignoreTernaryTests”Ignore ternary expressions that check for null or undefined. Default: false.
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "level": "on", "options": { "ignoreTernaryTests": true } } } } }}declare const x: string | null;const value = x !== null ? x : 'default';ignoreMixedLogicalExpressions
Section titled “ignoreMixedLogicalExpressions”Ignore || and ||= whose connected logical tree also contains a &&. Default: false.
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "level": "on", "options": { "ignoreMixedLogicalExpressions": true } } } } }}Invalid
Section titled “Invalid”|| and ||= are still reported when the surrounding logical tree does not contain &&.
declare const maybeString: string | null;const value = maybeString || 'default';/invalid-mixed-or.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || ‘default’;
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
declare let assigned: string | null;assigned ||= 'default';/invalid-mixed-or-assign.ts:2:10 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ??= instead of ||=.
1 │ declare let assigned: string | null;
> 2 │ assigned ||= ‘default’;
│ ^^^
3 │
ℹ The ||= operator assigns when the left side is falsy, while ??= only assigns when it is null or undefined.
ℹ 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.
|| and ||= mixed with && in the same logical tree are not reported.
declare const a: string | null;declare const b: string;const r = (a || 'default') && b;declare const b: string;declare let assigned: string | null;assigned ||= b && 'fallback';ignoreBooleanCoercion
Section titled “ignoreBooleanCoercion”Ignore || and ||= used inside a Boolean() call, where coalescing on
falsy values is intentional. Default: false.
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "level": "on", "options": { "ignoreBooleanCoercion": true } } } } }}Invalid
Section titled “Invalid”|| and ||= outside a Boolean() call are still reported.
declare const maybeString: string | null;const value = maybeString || 'default';/invalid-boolean-coercion.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || ‘default’;
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
|| and ||= inside a Boolean() call are not reported.
declare const a: string | null;declare const b: string;const r = Boolean(a || b);ignorePrimitives
Section titled “ignorePrimitives”Ignore ||, ||=, and ternary expressions when every non-nullish variant
of the operand is a primitive the option opts out of. Use true to ignore
all primitives, or an object selecting string, number, boolean, or
bigint. Default: none.
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "level": "on", "options": { "ignorePrimitives": { "string": true } } } } } }}Invalid
Section titled “Invalid”Primitive kinds that are not opted out of are still reported.
declare const count: number | null;const value = count || 0;/invalid-primitives.ts:2:21 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const count: number | null;
> 2 │ const value = count || 0;
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
A string operand is not reported when string is ignored.
declare const name: string | null;const value = name || 'default';ignoreIfStatements
Section titled “ignoreIfStatements”By default, Biome reports an if statement that only assigns to a
nullish variable, since it can be rewritten as ??=. Set this to true
to ignore those statements. Default: false.
{ "linter": { "rules": { "nursery": { "useNullishCoalescing": { "level": "on", "options": { "ignoreIfStatements": true } } } } }}Invalid
Section titled “Invalid”|| and ||= are still reported when only if statements are ignored.
declare const maybeString: string | null;const value = maybeString || 'default';/invalid-if-statements.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use ?? instead of ||.
1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || ‘default’;
│ ^^
3 │
ℹ The || operator checks for all falsy values (including 0, ”, and false), while ?? only checks for null and undefined.
ℹ 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.
An if statement performing a nullish assignment is not reported.
declare let a: { x: string } | null;declare function makeA(): { x: string };if (!a) { a = makeA();}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.