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

Selecting a case from a discriminated union using intersection does not work #22154

Closed
rklaehn opened this issue Feb 23, 2018 · 2 comments
Closed
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@rklaehn
Copy link

rklaehn commented Feb 23, 2018

TypeScript Version: 2.7.0-dev.201xxxxx

Search Terms:
discriminated union, narrowing, intersection

Code

type DU = { t: '1', a: 2 } | { t: '2', c: 3 }
type C1 = DU & { t: '1' }
declare const foo: C1
foo.a // <= does not compile

Expected behavior:
C1 should be inferred to { t: '1', a: 2 }, so it should be clear that a can be accessed

Actual behavior:
C1 is inferred to ({ t: "1"; a: 2; } & { t: "1"; }) | ({ t: "2"; c: 3; } & { t: "1"; })

Note the impossible type ({ t: "2"; c: 3; } & { t: "1"; }) is not removed!

Playground Link:
link

Related Issues:

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 24, 2018

I believe we discussed this at #18339. I believe the issue was two-fold:

  1. Some of these intersection types are vacuous, but it is computationally intensive to try to discover which types are uninhabitable.
  2. It becomes difficult to find out just how you got to a never type when you have an uninhabitable intersection.

@DanielRosenwasser DanielRosenwasser added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Feb 24, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants