-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Function return type is not inferred when used in generic position #10698
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
After spending time debugging through our code in type-inference and contextual typing and talking with @vladima, the fix for this one will not be trivia at all and would require re-architecture on how we are doing type-inference/contextual typing. What happen here is that when compiler is checking arrow function, in trying to figure out return type of the function, it will then request contextual signature and trying to figure out return type of the contextual signature. This is the part that make this code broke in 2.0 but not 1.8, in 2.0 we introduce concept of literal type in 2.0 so in knowing whether the return type of the arrow function should be literal type of base primitive type (e.g number string) we end up having to figure out whether such position is sensitive to literal contextual type which in turn causes as to have to figure out return type of the contextual signature and the circulation begins. |
Found another observation that probably relates to this.
|
I don't think this is that hard to fix, if I had read the code correctly. When asking for return type for the first time, compiler will not see Then the control flow comes back to the first time But |
Any update here? |
@HerringtonDarkholme refer to this comment
In other words, it's not going to be something we can fix anytime soon, assuming the investigation was correct. We'll review the PR to see if it is the right fix, though. |
I was experimenting with a fix for #15016 in this branch, I believe it solves this scenario as well: |
I don't want to put your work down, but this example seems to be working with the vanilla compiler as well - link. I'm not sure whether the issue is still present in more complex use-cases though. |
@gcnew You're right, I just blindly assumed the vanilla compiler was producing a compiler error for this scenario as described in the issue. The change I made is irrelevant here because no contextual typing is occurring. Perhaps the issue was already fixed somewhere else? I'm not seeing any compiler errors in 2.2.2 with noImplicitAny enabled. |
Duplicate of #9659 |
TypeScript Version: typescript@rc 2.0.2
Code
with
noImplicityAny
being trueExpected behavior:
It compiles.
id
is inferred asfunction id<() => number>(p: () => number): void
Actual behavior:
It does not compile.
Compiler error:
Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
p
is inferred as() => any
This code works in TS1.8.
The text was updated successfully, but these errors were encountered: