Skip to content

do not prefer ?? over || with boolean/nullish union - #30

Merged
lukasvice merged 1 commit into
mainfrom
ignore-boolean-with-nullish-coaleshing
Apr 30, 2026
Merged

do not prefer ?? over || with boolean/nullish union#30
lukasvice merged 1 commit into
mainfrom
ignore-boolean-with-nullish-coaleshing

Conversation

@lukasvice

@lukasvice lukasvice commented Apr 30, 2026

Copy link
Copy Markdown
Member

Configure ignorePrimitives for prefer-nullish-coalescing

The @typescript-eslint/prefer-nullish-coalescing rule is already enabled via strictTypeChecked.

This change adds ignorePrimitives: { boolean: true } to allow || for boolean union types (boolean | undefined, boolean | null), where || is often the intended operator.

For example, with optional React props typed as boolean | undefined:

// ✅ Allowed now — || intentionally treats both false and undefined as falsy
disabled={submitting || disabled}

Using ?? here would change the semantics: submitting ?? disabled returns false when submitting is false, while || falls through to disabled. For boolean flags, treating false and undefined the same way is typically what you want.

The rule still enforces ?? for non-boolean nullable types, where falsy coercion is usually a bug:

// ❌ Still an error — "" and 0 would incorrectly fall through with ||
const name = user.name || "Anonymous";
const count = response.count || 0;

// ✅ Correct
const name = user.name ?? "Anonymous";
const count = response.count ?? 0;

See https://typescript-eslint.io/rules/prefer-nullish-coalescing for reference.

@lukasvice lukasvice self-assigned this Apr 30, 2026
@lukasvice
lukasvice requested a review from alexlanz April 30, 2026 12:01
@lukasvice
lukasvice merged commit a98e99f into main Apr 30, 2026
@lukasvice
lukasvice deleted the ignore-boolean-with-nullish-coaleshing branch April 30, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants