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

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

Open
rixtox opened this issue May 18, 2023 · 0 comments
Open

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

rixtox opened this issue May 18, 2023 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@rixtox
Copy link

rixtox commented May 18, 2023

πŸ”Ž 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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants