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

Inferred type does not work for generic reduce function with when not used as a lambda #27708

Closed
Roaders opened this issue Oct 11, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@Roaders
Copy link

Roaders commented Oct 11, 2018

TypeScript Version: 3.1

Search Terms: type inferrence

Code

function reduceToPromise<T extends string>(promise: Promise<T>, items: string[]){
    return promise
}

const initialPromise: Promise<string> = {} as any;
const items: string[][] = [];

const reducedOne = items.reduce(reduceToPromise, initialPromise);
const reducedTwo = items.reduce((promise, items) => reduceToPromise(promise, items), initialPromise);

Playground

Expected behavior:
Both reducedOne and reducedTwo should be typed as Promise<string>

Actual behavior:
reducedOne is typed as {}

This issue only occurs when the function has a generic type. This works fine:

function reduceToPromise(promise: Promise<string>, items: string[]){
    return promise
}

const initialPromise: Promise<string> = {} as any;
const items: string[][] = [];

const reduced = items.reduce(reduceToPromise, initialPromise);

Playground

@ghost
Copy link

ghost commented Oct 11, 2018

Simplified example:

function id<T>(x: T): T { return x; }
function callIt<T, U>(f: (t: T) => U, x: T): U { return f(x); }
const x = callIt(id, 0); // Infers `callIt<{}, {}>` so `x` is `{}`. No error if order of parameters is reversed.

@weswigham weswigham added Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Oct 16, 2018
@weswigham
Copy link
Member

I think in the past we've called this a design limitation and told people to invert their parameter order; but because of what we've been looking at recently w.r.t. proper unification, I'll mark this as a bug instead and ping @ahejlsberg for input. You think this is worth reconsidering?

@ahejlsberg
Copy link
Member

I'd love to reconsider, but it is a hard problem. An inference scheme based on unification would presumably get this example right, but we know from experiments that unification surfaces hard problems for which we don't have an answer. See #24626 and other issues linked in that thread.

We could consider tweaks to the existing algorithm to defer inference from arguments that have a generic function type (similar to what we do for function expressions with contextually typed parameters), but that would only work for simple generic function arguments not for structures containing generic functions values. So, not sure that's worthwhile.

@DanielRosenwasser
Copy link
Member

Closing in favor of #9366.

@DanielRosenwasser DanielRosenwasser added Duplicate An existing issue was already created and removed Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Oct 25, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed for automatic house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants