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

Wrong type inference when type should be union typed #46641

Closed
ffMathy opened this issue Nov 2, 2021 · 10 comments
Closed

Wrong type inference when type should be union typed #46641

ffMathy opened this issue Nov 2, 2021 · 10 comments
Labels
Duplicate An existing issue was already created

Comments

@ffMathy
Copy link

ffMathy commented Nov 2, 2021

Bug Report

πŸ”Ž Search Terms

  • "Ternary if type inference"

πŸ•— 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:

{
    foo: string;
    bar: number;
} | {
    foo: string;
    bar?: undefined;
}

However, in the first example, the inferred type is TypeA instead of TypeA|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 as TypeA.

@MartinJohns
Copy link
Contributor

Issue template for bug reports.

@MartinJohns
Copy link
Contributor

Then the inferred type of x is incorrectly TypeA. The inferred type should have been TypeA|TypeB.

Subtype reduction kicks in here. Every TypeB is also a TypeA, because the properties match. It's the same as #46449.

You can avoid this behaviour by adding a type annotation for the return type.

@jcalz
Copy link
Contributor

jcalz commented Nov 2, 2021

Yeah, this is just subtype reduction and has been part of TypeScript for... (checks notes) ...ever? Or close to it.

@ffMathy
Copy link
Author

ffMathy commented Nov 2, 2021

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

@jcalz
Copy link
Contributor

jcalz commented Nov 2, 2021

@jcalz just because it has been part of TypeScript forever, doesn't mean it can't be improved and shouldn't be discussed.

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.

@ffMathy
Copy link
Author

ffMathy commented Nov 2, 2021

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

@jcalz
Copy link
Contributor

jcalz commented Nov 2, 2021

You're not sure how to do this?

πŸ”Ž Search Terms <!-- What search terms did you use when trying to find an existing bug report? List them here so people in the future can find this one more easily.

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.

πŸ’» Code <!-- Please post the relevant code sample here as well-->

You wrote N/A there because you weren't sure how to put the code sample in plaintext?

I think it is less readable and understandable now, but I get that you want consistency

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.

@ffMathy
Copy link
Author

ffMathy commented Nov 2, 2021

You're not sure how to do this?

πŸ”Ž Search Terms <!-- What search terms did you use when trying to find an existing bug report? List them here so people in the future can find this one more easily.

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.

πŸ’» Code <!-- Please post the relevant code sample here as well-->

You wrote N/A there because you weren't sure how to put the code sample in plaintext?

I think it is less readable and understandable now, but I get that you want consistency

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.

@jcalz
Copy link
Contributor

jcalz commented Nov 2, 2021

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!

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 2, 2021
@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.

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