Skip to content

Contextual typing does not flow backward through function return types to the function arguments #5256

Closed
@drarmstr

Description

@drarmstr

Consider the following case:

class Entry<T> {
    constructor(callback: (v: T) => number) { }
}
interface Widget {
    list: Entry<{ foo: number }>[];
}

var widget: Widget = {
    list: [
        new Entry((v) => v.foo), // Error accessing v.foo
    ]
};

We get a compiler error in the callback accessing v.foo that foo does not exist on type {} since {} is the default type when it is unable to infer the type. However, in this case we know from the Widget definition that T must be typed to {foo:number}. But, it appears TypeScript is not using this as an inference-site to properly determine T.

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