Skip to content

Broken type inference in generic-connected functions return typeΒ #48466

Closed
@risen228

Description

@risen228

Bug Report

πŸ”Ž Search Terms

generic function infer unknown type without specifying parameter type

πŸ•— Version & Regression Information

Faced it with 4.3.5, but it still occurs in 4.6.3 and Nightly (v4.7.0-dev.20220302)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Options<TParams, TDone, TMapped> {
    fetch: (params: TParams, foo: number) => TDone,
    map: (data: TDone) => TMapped
}

function example<TParams, TDone, TMapped>(options: Options<TParams, TDone, TMapped>) {
    return (params: TParams) => {
        const data = options.fetch(params, 123)
        return options.map(data)
    }
}

interface Params {
    one: number
    two: string
}

example({
    // WORKS
    fetch: (params: Params) => 123,
    map: (number) => String(number)
})

example({
    // WORKS
    fetch: (params: Params, foo: number) => 123,
    map: (number) => String(number)
})

example({
    // INFERS "unknown"
    fetch: (params: Params, foo) => 123,
    map: (number) => String(number)
})

πŸ™ Actual behavior

In 3rd example, without explicitly specifying foo parameter type, the return type of fetch function infers as unknown.
FYI: foo type infers correctly from Options inferface.

πŸ™‚ Expected behavior

The code 3rd example works like the first two examples, correctly infering the fetch function's return type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions