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

Wrong type inferred for indirect call of generic function #29638

Closed
tp opened this issue Jan 29, 2019 · 5 comments
Closed

Wrong type inferred for indirect call of generic function #29638

tp opened this issue Jan 29, 2019 · 5 comments
Labels
Fixed A PR has been merged for this issue

Comments

@tp
Copy link

tp commented Jan 29, 2019

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

@weswigham weswigham added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jan 29, 2019
@weswigham
Copy link
Member

weswigham commented Jan 29, 2019

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

@tp
Copy link
Author

tp commented Mar 5, 2019

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)

@tp
Copy link
Author

tp commented Mar 5, 2019

@RyanCavanaugh Why did you close this if the reported issue still persists?

@RyanCavanaugh
Copy link
Member

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

@ahejlsberg
Copy link
Member

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

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Mar 6, 2019
@RyanCavanaugh RyanCavanaugh removed the Design Limitation Constraints of the existing architecture prevent this from being fixed label Mar 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants