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
Proposal for Parameter Type Inference for immediately invoked anonymous functions #4142
I have to target ES3/ES5 and I quite often find myself writing something like this:
for(letroleTypeofthis.RoleTypes){((roleType)=>{//roleType is of type any, but should be typeof "outer" roleTypeobservable.subscribe((nv)=>{//Do something with it in a functionthis.afunction(nv,roleType2.RoleTypeID);});})(roleType);//Protect variable scope}
or something like this:
(($)=>{//Why not infer $ as typeof jQuery?})(jQuery);
I think it was reasonable to infer the parameter type from its only usage here
How do we avoid making this circular? We already contextually type function arguments by the type of their corresponding parameters.
(function(x){})(t=>t);
Here, to get the type of x, we would (under the proposal) get the type of t => t. But that type is already dependent on the type of x, so we have to get the type of x, which involves us getting the type of t => t, and around and around we go.
I didn't think of that... The only option would be to only infer x if the parameter value is not inferred which seems to be complicated and not really foreseeable...
Ryan Cavanaugh (@RyanCavanaugh) and I have talked about this offline. It is rather difficult to reason about precisely what you want to happen, but I think we can at least give some decent inference here. Also, I'd argue this is sufficiently prominent in JS code that we should consider it more.
For now you can always give a type annotation on the TS side. In JS, it seems like JSDoc comments on function expressions don't get picked up though?
@2426021684 Clearly it's possible in that type system. I think it is in ours as well, but it will possibly introduce some complexities. I know you probably don't mean it to come off in a bad way, but saying another type system can do it is pretty unhelpful, especially since there's no formal or technical proposal here.
Well, the for - loop thing is no longer valid anyway. The jQuery example was still valid.
If I understand correctly, the following is the problem:
(($)=>{})(jQuery: JQuery);(($: JQuery)=>{})(jQuery);// How to see if $ should be inferred from parameter or just check the parameter to have the right type?
Could we make a rule that if a parameter does not have a type annotation (meaning its type is any) and the method is immediately invoked, we infer it from usage.
But I do not know if this is worth the effort, I do not come across this very often.
Add a new contextual typing rule for parameters of a function, if its a function expression immediately wrapped in a call, use the type of the matching argument in the call expression argument list.
Open question, how does rest params behave?
Activity
RyanCavanaugh commentedon Aug 4, 2015
How do we avoid making this circular? We already contextually type function arguments by the type of their corresponding parameters.
Here, to get the type of
x, we would (under the proposal) get the type oft => t. But that type is already dependent on the type ofx, so we have to get the type ofx, which involves us getting the type oft => t, and around and around we go.aersam commentedon Aug 6, 2015
I didn't think of that... The only option would be to only infer x if the parameter value is not inferred which seems to be complicated and not really foreseeable...
DanielRosenwasser commentedon Apr 9, 2016
Ryan Cavanaugh (@RyanCavanaugh) and I have talked about this offline. It is rather difficult to reason about precisely what you want to happen, but I think we can at least give some decent inference here. Also, I'd argue this is sufficiently prominent in JS code that we should consider it more.
For now you can always give a type annotation on the TS side. In JS, it seems like JSDoc comments on function expressions don't get picked up though?
michaelmesser commentedon Apr 9, 2016
Flow can do it
DanielRosenwasser commentedon Apr 9, 2016
@2426021684 Clearly it's possible in that type system. I think it is in ours as well, but it will possibly introduce some complexities. I know you probably don't mean it to come off in a bad way, but saying another type system can do it is pretty unhelpful, especially since there's no formal or technical proposal here.
aersam commentedon Apr 11, 2016
Well, the for - loop thing is no longer valid anyway. The jQuery example was still valid.
If I understand correctly, the following is the problem:
Could we make a rule that if a parameter does not have a type annotation (meaning its type is any) and the method is immediately invoked, we infer it from usage.
But I do not know if this is worth the effort, I do not come across this very often.
mhegazy commentedon Apr 14, 2016
Add a new contextual typing rule for parameters of a function, if its a function expression immediately wrapped in a call, use the type of the matching argument in the call expression argument list.
Open question, how does rest params behave?