Skip to content

Nullish coalescing operator does not understand cases where at least one operand is guaranteed to not be null, but not which one is not null #61670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RubyTunaley opened this issue May 8, 2025 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@RubyTunaley
Copy link

RubyTunaley commented May 8, 2025

🔎 Search Terms

"nullish coalescing" "narrowing" "type narrowing" "if statement" "or expression"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about nullish coalescing and type narrowing.

⏯ Playground Link

Playground link with relevant code: https://www.typescriptlang.org/play/?target=6&ts=5.9.0-dev.20250507#code/MYewdgzgLgBAhgLhmArgWwEYFMBOMA+yKANsTALwwAM8E8YAngNwBQoksGSqmuBRpCtVr1mLAJYAzGAAo4MAITlKqQfkIZFygcQCUMAN4sAkO2gxg3dNjyV5AfnswMrAL4sgA

💻 Code

const a: number | null = 0 as any;
const b: number | null = 0 as any;
if (a !== null || b !== null) {
	const c: number = a ?? b;
}

🙁 Actual behavior

The type checker generates an error when assigning to c: Type 'number | null' is not assignable to type 'number'.
Type 'null' is not assignable to type 'number'.

This is incorrect because the if statement guarantees that at least one of a or b is not null.

This also happens with code that excludes rather than narrows, e.g.:

const a: number | null = 0 as any;
const b: number | null = 0 as any;
if (a === null && b === null) {
	throw new Error();
}
const c: number = a ?? b;

🙂 Expected behavior

No error should occur because the if statement guarantees that at least one of a or b is not null. If the code was modified so that an explicit type was not specified, c should be inferred as number.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #49452 and a few others. Search terms: coalesce in:title

@RyanCavanaugh
Copy link
Member

See #61578, #48536, etc -- narrowing isn't equipped to do the sort of constraint solving exercise this code pattern would require.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 8, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants