镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Type inference for generic functions #11537

Description

TypeScript Version: 2.0.3

Code

function id<T>(v: T): T { return v; }

function applyToNumber<T>(v: T, toNumber: (x: T) => number): number {
    return toNumber(v);
}

applyToNumber(5, v => v); // ok
applyToNumber(5, id); // ok
applyToNumber("aaa", v => v); // error, as expected.
applyToNumber("aaa", id); // ok, but an error is expected.

Expected behavior:
error

Actual behavior:
no error.

Activity

  1. zpdDG4gta8XKpMCd commented on Oct 11, 2016

    @zpdDG4gta8XKpMCd

    might be dup a new shiny case of #3038

  2. sergey-shandar commented on Oct 11, 2016

    @sergey-shandar
    ContributorAuthor

    If it's compatibility problem, it would be really helpful to have a "strictTyping" compilation mode which would prohibit converting the any type to other types without explicit cast.

  3. zpdDG4gta8XKpMCd commented on Oct 11, 2016

    @zpdDG4gta8XKpMCd

    they say without any it will be an infinite recursion which i don't get because an explicit lambda (v => v in your case) doesn't have this problem, if so.. why would not TS handle all function references as explicit lambdas behind the scenes to avoid this recursion problem

  4. sergey-shandar commented on Oct 11, 2016

    @sergey-shandar
    ContributorAuthor

    An ugly workaround

    function getId<T>(): (v: T) => T { return v => v; }
    
    applyToNumber(5, getId()); // error, can't infer the 'T' type.
    applyToNumber(5, getId<number>()); // ok but 'v => v' looks better.
  5. mhegazy commented on May 18, 2017

    @mhegazy
    Contributor

    related to #5616, and possibly #417, #5487, #9107, #9659, #9660,
    #10247, #11311, #11343, #5616

  6. zpdDG4gta8XKpMCd commented on May 18, 2017

    @zpdDG4gta8XKpMCd

    looks like its time to bite that bullet or else you gonna break that all time record of the number of open issues related to the same problemo

  7. AllainPL commented on Nov 13, 2017

    @AllainPL

    The sample given:

    applyToNumber("aaa", id); // ok, but an error is expected.

    in TS 2.6 returns error:

    Argument of type '<T>(v: T) => T' is not assignable to parameter of type '(x: string) => number'.
      Type 'string' is not assignable to type 'number'.
    

    So I assume it's fixed.

  8. added
    FixedA PR has been merged for this issue
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Nov 13, 2017
  9. mhegazy commented on Nov 13, 2017

    @mhegazy
    Contributor

    thanks! closing.

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

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions