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

Nested discriminated union inference #30233

Closed
jeremybparagon opened this issue Mar 5, 2019 · 3 comments
Closed

Nested discriminated union inference #30233

jeremybparagon opened this issue Mar 5, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@jeremybparagon
Copy link

TypeScript Version: 3.4.0-dev.20190305

Search Terms: nested discriminated union

Code

interface A {
  z: 'a';
}
interface B {
  z: 'b';
}
interface C {
  y: A | B;
}
interface D {
  y: A;
}
const x = (c: C) => {
  if (c.y.z === 'a') {
    const w: A = c.y;
    const v: D = c;
  }
};

Expected behavior: The code compiles successfully.

Actual behavior: The assignment to v fails, with a message that Type 'C' is not assignable to type 'D'.

Playground Link: https://www.typescriptlang.org/play/#src=interface%20A%20%7B%0D%0A%20%20z%3A%20'a'%3B%0D%0A%7D%0D%0Ainterface%20B%20%7B%0D%0A%20%20z%3A%20'b'%3B%0D%0A%7D%0D%0Ainterface%20C%20%7B%0D%0A%20%20y%3A%20A%20%7C%20B%3B%0D%0A%7D%0D%0Ainterface%20D%20%7B%0D%0A%20%20y%3A%20A%3B%0D%0A%7D%0D%0Aconst%20x%20%3D%20(c%3A%20C)%20%3D%3E%20%7B%0D%0A%20%20if%20(c.y.z%20%3D%3D%3D%20'a')%20%7B%0D%0A%20%20%20%20const%20w%3A%20A%20%3D%20c.y%3B%0D%0A%20%20%20%20const%20v%3A%20D%20%3D%20c%3B%0D%0A%20%20%7D%0D%0A%7D%3B%0D%0A

@jack-williams
Copy link
Collaborator

I think this is a duplicate of #18758

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 5, 2019
@jeremybparagon
Copy link
Author

Thanks, @jack-williams, I figured someone must have run into this before but couldn't find it. This seems like an even simpler scenario, since here we would really only need the conclusion about y that we already deduce on the previous line to check that the assignment to v should be accepted, as opposed to using the nested discriminant to choose between different possible properties of the parent object c, the kind of thing desired in #18758.

@typescript-bot
Copy link
Collaborator

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

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