Skip to content

Comma operator seems to confuse nullish coalescingΒ #60401

Closed
@kbjr

Description

πŸ”Ž 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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions