Skip to content

Type inference does not work when in return position #34929

Closed
@jerguslejko

Description

@jerguslejko

TypeScript Version: 3.7-beta

Search Terms: inference, generic functions

Code

export function foo<T, K>(props: {
    seed: T;

    producer: (x: T) => K;

    consumer: (data: K) => void;
}) {
    //
}

foo({
    seed: "foo",

    producer: (seed) => {
        // type of `seed` is string
        return seed; 
    },

    consumer: (data) => {
        // type of `data` is unknown 
    }
})

Expected behavior: TypeScript infers K to a string, so the type of data is string.

Actual behavior: data variable is unknown.

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