Skip to content
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

Comma operator seems to confuse nullish coalescing #60401

Closed
kbjr opened this issue Nov 3, 2024 · 0 comments Β· Fixed by #60402
Closed

Comma operator seems to confuse nullish coalescing #60401

kbjr opened this issue Nov 3, 2024 · 0 comments Β· Fixed by #60402
Labels
Bug A bug in TypeScript
Milestone

Comments

@kbjr
Copy link

kbjr commented Nov 3, 2024

πŸ”Ž Search Terms

comma operator nullish coalescing never

πŸ•— Version & Regression Information

  • This changed between versions 5.5.4 and 5.6.3

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.3#code/FAEwpgxgNghgTmABBA9gOwM4BdEFsYCeARmAFyJZwCuSAPomlVFANzDBRg4CWiAvIgAMbYAG9giScnTZEMfnkIlEAfhUVqYNoglTUmHEQUAKbgGozAGkXEwASm16ZOCAqNqNNEVOkHEIE3MrGxI7VXVKL2AAX3ZgIA

πŸ’» Code

declare const maybe: true | null;

let i = 0;

// This works
const a = maybe ?? true;

// This works
const b = (i++, maybe);
const c = b ?? true;

// This fails
const d = (i++, maybe) ?? true;

πŸ™ Actual behavior

A build error occurs:

Right operand of ?? is unreachable because the left operand is never nullish. ts(2869)

πŸ™‚ Expected behavior

The code should compile without error; The left side of the ?? can still be nullish in the const d case.

The const b / const c case is functionally identical and is recognized to be valid

Additional information about the issue

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants