You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiler is unable to resolve return type of function even though it is returning function with known return type #26623
TS7023: 'isNameSameAsNickname' 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.
Comment: This error appears to be incorrect (or at least worded ambiguously) because isNameSameAsNickname is not referenced directly or indirectly in one of its return expressions. Specifically, isNameSameAsNickname does not appear directly in the return expression compareNames(this) nor does it appear indirectly in identifiable.name === identifiable.nickname.
changed the title [-]Returning a function with a known return type[/-][+] Compiler is unable to resolve return type of function even though it is returning function with known return type[/+]on Aug 23, 2018
This is a circular operation, because we have to check what the type of Person#isNameSameAsNickname is in order to check the call to compareNames to figure out what the return type could be. You can see this because removing isNameSameAsNickname from Identifiable causes the error to go away.
Thanks for your reply. So it sounds like you are saying, the compiler is undergoing the following "thought" process:
Okay, so isSameAsNickname depends on compareNames.
And compareNames requires an Identifiable as input.
And an Identifiable must have a property, isNameSameAsNickname, that returns a boolean.
Therefore, compareNames depends on isNameSameAsNickname.
Damn, this is complicated, I give up.
Meanwhile, the human being (me) is undergoing a different thought process:
Okay, so isNameSameAsNickname depends on compareNames.
And compareNames returns a boolean.
Therefore isNameSameAsNickname must return a boolean.
Therefore Person is a legitimate Identifiable.
Therefore compareNames can be called.
This suggests that the order that type checks are performed has a significant effect on whether dependencies resolve or are deemed circular. On the surface, this seems like it shouldn't be hard to fix, but I gather it's a difficult problem or it would already be solved.
This does bring up a question...
The workaround I provided is obviously very simple to implement, so I'm OK using that, but is there a better, more "best practices" way to accomplish this type of thing that avoids this issue altogether?
I gather it's a difficult problem or it would already be solved.
Yep 😉. The real issue here is that the two main operations the checker does - inference and error detection - occur at the "same time". You could imagine a different world where all inference happens, then all error checking happens, which would avoid this problem because the check of whether the argument type is assignable to the parameter type would only be part of the error-checking phase. But this would likely be at least twice as slow as the current implementation.
There's no set practice that will avoid all circularity issues. The return type annotation is the best alternative for this example, I would say.
Reacted by Damien Golding, Joel Nordström and Wessel Kronemeijer
Activity
[-]Returning a function with a known return type[/-][+] Compiler is unable to resolve return type of function even though it is returning function with known return type[/+]RyanCavanaugh commentedon Aug 23, 2018
This is a circular operation, because we have to check what the type of
Person#isNameSameAsNicknameis in order to check the call tocompareNamesto figure out what the return type could be. You can see this because removingisNameSameAsNicknamefromIdentifiablecauses the error to go away.devuxer commentedon Aug 23, 2018
Ryan Cavanaugh (@RyanCavanaugh),
Thanks for your reply. So it sounds like you are saying, the compiler is undergoing the following "thought" process:
isSameAsNicknamedepends oncompareNames.compareNamesrequires anIdentifiableas input.Identifiablemust have a property,isNameSameAsNickname, that returns a boolean.compareNamesdepends onisNameSameAsNickname.Meanwhile, the human being (me) is undergoing a different thought process:
isNameSameAsNicknamedepends oncompareNames.compareNamesreturns a boolean.isNameSameAsNicknamemust return a boolean.Personis a legitimateIdentifiable.compareNamescan be called.This suggests that the order that type checks are performed has a significant effect on whether dependencies resolve or are deemed circular. On the surface, this seems like it shouldn't be hard to fix, but I gather it's a difficult problem or it would already be solved.
This does bring up a question...
The workaround I provided is obviously very simple to implement, so I'm OK using that, but is there a better, more "best practices" way to accomplish this type of thing that avoids this issue altogether?
RyanCavanaugh commentedon Aug 24, 2018
Yep 😉. The real issue here is that the two main operations the checker does - inference and error detection - occur at the "same time". You could imagine a different world where all inference happens, then all error checking happens, which would avoid this problem because the check of whether the argument type is assignable to the parameter type would only be part of the error-checking phase. But this would likely be at least twice as slow as the current implementation.
There's no set practice that will avoid all circularity issues. The return type annotation is the best alternative for this example, I would say.
add type annotations to methods that have an implicit any type in TS 4.3
feat(typescript): update to typescript 4.3.5 (#3103)