Skip to content

Union types are not being narrowed down correctly in 4.3Β #44401

Closed
@KnorpelSenf

Description

@KnorpelSenf

Bug Report

Potentially related to #44382.

πŸ”Ž Search Terms

  • union types
  • narrowing
  • 4.3

πŸ•— Version & Regression Information

  • This changed between versions 4.2 and 4.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface X {
  a?: { aProp: string };
  b?: { bProp: string };
}
type AorB = { a: object; b: undefined } | { a: undefined; b: object };

declare const q: X & AorB;

if (q.a !== undefined) {
  q.a.aProp;
} else {
  // q.b is incorrectly inferred as potentially undefined:
  q.b.bProp;
}

πŸ™ Actual behavior

q.b is inferred as potentially undefined in the else block. Given that q conforms with AorB, and q.a is known to be undefined in the else block, this is incorrect.

πŸ™‚ Expected behavior

q.b should be inferred as present. In particular, it should be { bProp: string }.

Metadata

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