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

Discriminated union not narrowing when discriminator is itself a union #54370

Closed
Peeja opened this issue May 24, 2023 · 5 comments
Closed

Discriminated union not narrowing when discriminator is itself a union #54370

Peeja opened this issue May 24, 2023 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@Peeja
Copy link
Contributor

Peeja commented May 24, 2023

Bug Report

πŸ”Ž Search Terms

discriminated union, discriminator, narrowing

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about discriminated unions

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type DiscriminatedUnion = A | B | CorD;

interface A {
  type: "a";
}

interface B {
  type: "b";
}

interface CorD {
  type: "c" | "d";
}

declare const thing: DiscriminatedUnion;

thing.type;
// ^? const thing: DiscriminatedUnion
thing.type;
//    ^? (property) type: "a" | "b" | "c" | "d"

if (thing.type === "a") throw "No good!";

thing.type;
// ^? const thing: B | CorD
thing.type;
//    ^? (property) type: "b" | "c" | "d"

if (thing.type === "c" || thing.type === "d") throw "No good!";

thing.type;
// ^? const thing: B | CorD
thing.type;
//    ^? (property) type: "b"

πŸ™ Actual behavior

thing.type at the end has type B | CorD.

πŸ™‚ Expected behavior

thing.type at the end should have type B, because it can never be CorD.


This may well be considered a feature that hasn't been implemented yet.

For context, here's the type I'm trying to remove. This is an AST for SQL expressions. I've parsed an expression using this library, which gives me an Expr, but I'm only expecting to handle simpler kinds of SQL expressions, and I'd like to throw on more complex ones and narrow the type. Currently, it's harder than I'd like to kick SelectFromUnion out of the large Expr union, because its type is 'union' | 'union all'. Probably my best bet at this point would be a custom type guard, but it sure would be nice if TS could figure out the narrowing for me.

@MartinJohns
Copy link
Contributor

Duplicate of #31404.

@Peeja
Copy link
Contributor Author

Peeja commented May 24, 2023

@MartinJohns Thanks!

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label May 26, 2023
@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.

2 similar comments
@microsoft-github-policy-service

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

@microsoft-github-policy-service

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