-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
π 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
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript