-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Return type inference does not infer union #32237
Comments
Smaller repro, |
Interestingly enough, if you replace, return arg; with, return {
someType : "",
subObject : {
prop0 : 0,
}
}; The inferred return type of |
This is normal subtype reduction - the type |
Facepalms It makes for unsound code in this case but soundness was never a goal. |
Wait, that doesn't explain why changing the return statement to a different form makes it infer the union. Both Or is the key phrase here "most inference operations"? And it just so happens that using a different kind of return statement changes the inference? Pretty unintuitive, and it doesn't seem like I should rely on the behavior. I guess an explicit return type annotation is better in this case? |
Maybe due to object literal freshness? |
You're probably right but I wonder if it was a conscious decision and why. It seems like a rather obscure rule. |
TypeScript Version: 3.5.1, 3.3.3
Search Terms:
return type, infer, union
Code
Expected behavior:
wtf
should be a union of types.Actual behavior:
wtf
is not a union of types.Playground Link: Playground
Related Issues:
I'll keep searching but I couldn't find anything. There were a bunch with the same keywords but they seemed unrelated, focusing on literals and generics.
The text was updated successfully, but these errors were encountered: