Skip to content

Type inference for Function.call return type on generic functionsΒ #54319

Open
@rixtox

Description

@rixtox

πŸ”Ž Search Terms

type infer function call generic

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function numberEcho(x: number): number {
    return x;
}

function genericEcho<T>(x: T): T {
    return x;
}

// This works
const resultNumber: number = numberEcho.call(this, 1);

// Type Error: Type 'unknown' is not assignable to type 'string'.(2322)
const resultString: string = genericEcho.call(this, 'hello');

πŸ™ Actual behavior

Type inference of genericEcho.call return type failed even when all the argument types are known at call site.

Of course explicitly hinting the return type would work but it's not good:

const resultString: string = genericEcho.call<typeof this, [string], string>(this, 'hello');

πŸ™‚ Expected behavior

I would expect type inference of generic function return type to work if all arguments types are known.

I think #40179 is related but the example I provided here is much more common since it exists in standard library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions