Skip to content

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

Closed
@Roaders

Description

@Roaders

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions