-
Notifications
You must be signed in to change notification settings - Fork 12.8k
property accessor creates intersection type instead of union #31694
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
Can narrow the versions down here more: this worked correctly in The conditional workaround does not work in our case because we’re dealing with a generic, so we can’t narrow like that. We have resorted to |
This is caused by #30769, and I think this is working as intended. An intersection is inferred for The narrowing you apply to The reason the |
Then this is a huge problem, because it’s “sound” and “conservative” to the point that we can’t use it. But if we reverse the order of things to avoid property access— |
@jack-williams thanks for explaining, that makes sense.
So, the problem is that the expression returns a union type BTW is it expected to have breaking changes in minor version changes? Then I need to read the changelog more carefully from now on 🤓 |
See #14116 |
Those two signatures are significantly different, and the error in the first is warranted. The first says that the The second says that The reason the second works is because the direction of |
Yes exactly! You would need a dependent type or stronger reasoning with conditional types. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.5.1 (also happens with
@next
)Search Terms: ternary, ternary intersection/union, property accessor intersection/union
Code
or
Expected behavior:
Works (it did up to version 3.4.5).
Actual behavior:
An intersection type is inferred for
foobar[key]
(orthis[key]
), however I think it should be a union type.Playground Link:
Useless because this used to work up to version 3.4.5 (and the playground is stuck at version 3.3.3).
Related Issues:
Maybe #9239
If I use an if/else instead of the ternary operator, it works because it's easier to figure out the type of
this[key]
:The text was updated successfully, but these errors were encountered: