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

union type is unexpectedly narrowed by ternary #46642

Closed
oatkiller opened this issue Nov 2, 2021 · 3 comments
Closed

union type is unexpectedly narrowed by ternary #46642

oatkiller opened this issue Nov 2, 2021 · 3 comments

Comments

@oatkiller
Copy link

Bug Report

🔎 Search Terms

ternary union narrow

🕗 Version & Regression Information

Verified using the playground with versions 3.3.3 through 4.4.4. Also tried nightly. I reviewed the common-bugs-that-arent-bugs comment for similar issues.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type narrowing / guarding (I skimmed the whole page.)

⏯ Playground Link

Playground link with relevant code

💻 Code

type A = { foo: string;}
type B = A & { bar : string;}

declare const binding: A | B;

// type is `A` but was expected to be `A | B`
const secondBinding = true ? binding : binding;

if ('bar' in secondBinding) {
  secondBinding.bar;
}

🙁 Actual behavior

binding has type A | B and was assigned to secondBinding. secondBinding has type A, but its type should've been inferred as the same type: A | B.

🙂 Expected behavior

secondBinding should have the same type as binding because it's assigned that way.

@oatkiller
Copy link
Author

The behavior is equivalent if A and B are interfaces.

@MartinJohns
Copy link
Contributor

Duplicate of ##46449.

@oatkiller
Copy link
Author

@MartinJohns Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants