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

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

Closed
risen228 opened this issue Mar 28, 2022 · 2 comments Β· Fixed by #48538
Closed

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

risen228 opened this issue Mar 28, 2022 · 2 comments Β· Fixed by #48538
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@risen228
Copy link

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.

@RyanCavanaugh
Copy link
Member

See #47599

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 28, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for 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 Fix Available A PR has been opened for this issue
Projects
None yet
3 participants