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 case with a union of discriminators not correctly excluded #54369

Closed
SanderRonde opened this issue May 24, 2023 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@SanderRonde
Copy link

Bug Report

πŸ”Ž Search Terms

discriminated union

πŸ•— Version & Regression Information

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

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

enum FooType {   Foo,   Bar,   Baz }

interface Foo{
  foo: number;
  type: FooType.Foo
}

interface BarBaz{
  type: FooType.Bar | FooType.Baz
}

function test(t: Foo | BarBaz) {
  if (t.type === FooType.Bar || t.type === FooType.Baz) {
    return
  }
  // t should only be able to be Foo here, but it can be Foo|BarBaz still.
  t.foo;

  // Alternatively this should work and we should be able to return to exclude the bar-baz case again,
  // but TS knows that `t.type` can not be either of these.
  if (t.type === FooType.Bar || t.type === FooType.Baz) {
    return;
  }
}

πŸ™ Actual behavior

The type of t is Foo | BarBaz while all possible cases of it being BarBaz have been excluded.

πŸ™‚ Expected behavior

The type of t is Foo.

I can imagine that this might be quite hard to fix and might have been a design (or priority) choice, but I figured I'd still submit it since you might not have seen it yet.

@Peeja
Copy link
Contributor

Peeja commented May 24, 2023

Huh. Jinx! πŸ˜€

@MartinJohns
Copy link
Contributor

Duplicate of #31404.

@SanderRonde
Copy link
Author

Ah indeed a duplicate, will close

@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.

5 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.

@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.

@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

5 participants