Skip to content

Generic inference to unknown after using function argumentsΒ #43371

Closed
@misha-erm

Description

@misha-erm

Bug Report

Hello πŸ‘‹πŸ»
I'm facing some strange issue with Generics. Until I specify input arguments in function all generics are inferred correctly. But as soon as I mention argument in function the inference is broken.

Thanks in advance for any help πŸ™πŸ»

πŸ”Ž Search Terms

  • generic is lost after specifying input arguments
  • typescript generic unknown after specifying arguments (found this stackoverflow but not sure if it's the same case)

πŸ•— Version & Regression Information

typescript: 4.2.3
Also tried nightly build but the issue remains

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Generics_____

⏯ Playground Link

Workbench Repro

πŸ’» Code

type Connector = <
    AccessToken
>(opts: {
    getAccessToken(input: {code: string}): Promise<AccessToken>;
    validateAuth(input: {fields: AccessToken}): Promise<{name: string}>;
}) => any;

const connector: Connector = (inp) => undefined

connector({
    getAccessToken: async () => ({token: 'token'}),
    validateAuth: async ({fields}) => { //fields: {token: string}
        //                  ^?
        return {name: 'qwe'}
    }
})

connector({
    getAccessToken: async (inp) => ({token: 'token'}), // mention input argument breaks inference
    validateAuth: async ({fields}) => { // fields: unknown
        //                  ^?
        return {name: 'qwe'}
    }
})

πŸ™ Actual behavior

fields inside validateAuth infer to unknown after using function argument in getAccessToken. Until I use arguments everything works OK

πŸ™‚ Expected behavior

Usage of function arguments inside getAccessToken should not affect Generic inference since they are statically type

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions