-
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
Proposal for Parameter Type Inference for immediately invoked anonymous functions #4142
Comments
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 |
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... |
@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? |
Flow can do it |
@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. (($) => {
})(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. |
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. |
I have to target ES3/ES5 and I quite often find myself writing something like this:
or something like this:
I think it was reasonable to infer the parameter type from its only usage here
The text was updated successfully, but these errors were encountered: