Skip to content

Proposal for Parameter Type Inference for immediately invoked anonymous functions #4142

Closed

Description

@aersam

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

Activity

  1. RyanCavanaugh commented on Aug 4, 2015

    @RyanCavanaugh
    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.

  2. aersam commented on Aug 6, 2015

    @aersam
    Author

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

  3. added
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    and removed on Aug 6, 2015
  4. added
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    and removed
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    on Apr 9, 2016
  5. DanielRosenwasser commented on Apr 9, 2016

    @DanielRosenwasser
    Member

    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?

  6. michaelmesser commented on Apr 9, 2016

    @michaelmesser

    Flow can do it

  7. DanielRosenwasser commented on Apr 9, 2016

    @DanielRosenwasser
    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.

  8. aersam commented on Apr 11, 2016

    @aersam
    Author

    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.

  9. added and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Apr 11, 2016
  10. mhegazy commented on Apr 14, 2016

    @mhegazy
    Contributor

    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?

  11. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions