Skip to content
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

Closed
aersam opened this issue Aug 4, 2015 · 7 comments
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@aersam
Copy link

aersam commented Aug 4, 2015

I have to target ES3/ES5 and I quite often find myself writing something like this:

for (let roleType of this.RoleTypes) {
    ((roleType) => {
        //roleType is of type any, but should be typeof "outer" roleType
            observable.subscribe((nv) => {//Do something with it in a function
                this.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

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Aug 4, 2015
@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Aug 4, 2015
@RyanCavanaugh
Copy link
Member

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.

@aersam
Copy link
Author

aersam commented 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...

@aersam aersam closed this as completed Aug 6, 2015
@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Aug 6, 2015
@DanielRosenwasser DanielRosenwasser added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed Too Complex An issue which adding support for may be too complex for the value it adds labels Apr 9, 2016
@DanielRosenwasser
Copy link
Member

@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
Copy link

Flow can do it

@DanielRosenwasser
Copy link
Member

@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
Copy link
Author

aersam commented 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:

(($) => {
})(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.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Apr 11, 2016
@mhegazy mhegazy added Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels Apr 14, 2016
@mhegazy
Copy link
Contributor

mhegazy commented 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?

@sandersn sandersn added the Fixed A PR has been merged for this issue label May 9, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants