-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Wrong type inference when type should be union typed #46641
Comments
Subtype reduction kicks in here. Every You can avoid this behaviour by adding a type annotation for the return type. |
Yeah, this is just subtype reduction and has been part of TypeScript for... (checks notes) ...ever? Or close to it. |
@jcalz just because it has been part of TypeScript forever, doesn't mean it can't be improved and shouldn't be discussed. @MartinJohns thank you. Is there anything that can be done about it though? Forgive my lack of knowledge in the TypeScript compiler, but I can't wrap my head around why this wouldn't be possible. I couldn't find an explanation for this in the issue you linked either. Is it because removing or allowing for disabling subtype reduction presents other issues? Is it possible (if it's not too much to ask) for an ELI5 or something similar on this? Perhaps it would clear out confusion for others too. |
It's a feature, not a bug. Are you filing a bug report or a feature suggestion? There's an issue template you should be filling out either way. Right now the onus is on you to do the things the TS team expects for someone filing an issue, before you can expect too much engagement here, unfortunately. |
@jcalz I filled out the issue template now. Sorry about that. The reason I chose to omit it, was because I wasn't sure how to fill out the individual fields in the template. I think it is less readable and understandable now, but I get that you want consistency. Also, I had no intention of just skipping everything. That's also why I made two examples in the playground, which I thought were much more self-explanatory. |
You're not sure how to do this?
When you file a bug here you should first be searching for existing issues that report the same thing so that you don't create a duplicate. Then, when you fail to find an existing issue that is the same, you list those search terms here, so that people in the future can find your issue when they are looking. If you leave required fields blank, the TS team will be less likely to do much more than close it as unactionable.
You wrote N/A there because you weren't sure how to put the code sample in plaintext?
Note that I'm just a nosy bystander and not a member of the TS team so you don't have to take anything I say as authoritative. I do like me some consistency, though. |
I filled in the search terms now. Missed it when going through the template. I wrote N/A because the two playground links have more than enough sufficient code to show what the point is. In fact, more than should be necessary. And I wouldn't call it being "nosy". I'd call it being plain arrogant. I get that you're probably a good help to the TypeScript team by catching naughty people who do not comply with the templates and what not (and I changed my report too to comply), but you don't have to do it in a hostile way. |
I think hostility/arrogance must be in the eye of the beholder, but itβs probably more productive at this point for me to bow out of this conversation and leave the issue in the hands of those whose behavior I hope youβll find more palatable. Good luck to you! |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
π Version & Regression Information
This seems to appear on all versions, so unsure if it's a bug actually. I just find the behavior "weird".
β― Playground Link
Example 1: https://www.typescriptlang.org/play?ts=4.4.4#code/C4TwDgpgBAKuECEoF4oG8BQVtQGYHt8AuKAZ2ACcBLAOwHMBuLHAIwEMKSaBXAWxYgUmAXwwZQkWPACCKdM2wFiZSrUYZRY3NxoBjYFXw0odCMACSNcmz0QAFBzokWhADYQbASkw4oVXA4UdN4KvlAUZtwUxj5hcUokAEQA1gCOAO4AJmmZ6cmJADShcdjsnFAAjADMVQDsxTjCUGykUpAITL5NEK6k0GhQDdgRwFExQ2EJUIlKiRNNLW0Q0p2NGmLuwFAAHnKmFlbANrr2lNwQngxAA
Example 2: https://www.typescriptlang.org/play?ts=4.4.4#code/GYVwdgxgLglg9mABAcwKZQJJgM5QIaSoAUeATsgFyIBGccANqgQJQDeAUIl4jMCeW07dhpdCFJIOw6d2B0qAIgDWARwDuAE1Ua1ShQBohM4dTJUAjAGZLAdiPSAvgG57DxKnrZUiVonsixCR9-Yzk4RTCFEK5nV3YHdnZGKEQAD0QAXhR0LFwCCGIoUhBUZicgA
π» Code
N/A
π Actual behavior
In the second Playground example, the inferred type of
x
is correctly:However, in the first example, the inferred type is
TypeA
instead ofTypeA|TypeB
, which I think is odd.π Expected behavior
As in the second Playground example, I want the inferred type of the first example to be
TypeA|TypeB
, but it infers it asTypeA
.The text was updated successfully, but these errors were encountered: