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

Wrong type inferred for indirect call of generic function  #29638

Description

@tp

TypeScript Version: 3.2.2

Search Terms: Generics, lookahead, type inference

Code

function call<P1, R>(f: (p1: P1) => R, p1: P1): R {
    return f(p1);
}

function identity<T>(o: T): T {
    return o;
}

const x = call(identity, 1);


// When the parameters are reversed, it works:

function callReversed<P1, R>(p1: P1, f: (p1: P1) => R): R {
    return f(p1);
}

const y = callReversed(1, identity);

Expected behavior:

x is of type number

Actual behavior:

x is of type {}
only y is of type number

Playground Link: Playground link

Related Issues: I didn't find anything related, but maybe I have been using the wrong search term ("lookahead").

Activity

  1. weswigham commented on Jan 29, 2019

    @weswigham
    Member

    We don't do full unification - we match parameters from left to right (which is why reversing the order fixes the issue). It's a known design limitation - we've talked about improving it before (you can probably find related discussions by searching for topics about higher order functions).

  2. tp commented on Mar 5, 2019

    @tp
    Author

    Would this also be fixed by #30215?

    Edit: Ah, I guess not, because that PR states:

    In particular, it only delivers the desired outcome when types flow from left to right.

    Indeed this won't be supported yet with that change: #30215 (comment)

  3. tp commented on Mar 5, 2019

    @tp
    Author

    Ryan Cavanaugh (@RyanCavanaugh) Why did you close this if the reported issue still persists?

  4. RyanCavanaugh commented on Mar 5, 2019

    @RyanCavanaugh
    Member

    Timm Preetz (@tp) Issues are closed if there isn't an identifiable next set of work to do. In this case, we believe the only next step would be a near-complete rewrite of generic inference, which is already tracked at #30134.

  5. ahejlsberg commented on Mar 6, 2019

    @ahejlsberg
    Member

    Actually, this issue is fixed with the latest commits in #30215.

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