-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Discriminating a type union results in unexpected possible unsoundness #46730
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
Comments
Your variable |
@MartinJohns, thank you, this explains the case with |
Subtype reduction strikes again. This example looks pretty unfortunate though 😕 |
I don’t think there’s anything we can do here. The ternary does subtype reduction on the union of the narrowed type and the unnarrowed type of so there’s nothing new here, just two things working as expected that combine to look very strange in this case. |
Thank you for your investigation, @andrewbranch! That sounds like a very precise explanation of what’s happening. This helps me find this to be a duplicate of #46449. |
Bug Report
🔎 Search Terms
type guard, property checking, discriminated, narrowing, union, property existence, type assertion,
I’m sorry if this is a duplicate, I could not come up with a clear way to name this behavior and recognize it in the list of issues.
🕗 Version & Regression Information
Earliest version tested: 3.3.3333
Last version tested: 4.6.0-dev.20211105
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
No error on the last line.
🙂 Expected behavior
Error on the last line.
x ? b : b
andb
should be of the same type and behave the same (ignoring side-effects inx
). On the last line of the example the type ofb
in the “if-true” branch includes the optional field, but the type of the entire expression"optional" in b ? b : b
is{ id: string }
with the optional field missing!The text was updated successfully, but these errors were encountered: