-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
ReturnType<> of function intersection inconsistent. #31601
Comments
I think this is working as intended. Conditional type inference is done from the last overload not the first:
|
Hmmm, If the intention is to get the "most permissive catch-all case", wouldn't using the union of return types of each individual overload be better? I'm pretty sure I've written overloads where each overload returned different types, and the last one wasn't a "permissive catch-all". Especially for generic functions, where allowing such an overload would makes type checks useless, because the result would be something like I guess I'll just treat this as a gotcha' and be careful with how I structure overloads from now on |
I’m assuming by the “last overload”, it means the implementation signature, which is the most permissive case by definition. |
I just tested it, function foo(x : 1): number;
function foo(x : 2): string;
function foo(x : 1|2): number | string {
throw new Error("Not implemented");
}
//This is `string`
type r = ReturnType<typeof foo>;
type ReturnType_1<T> = (
T extends (x: 1) => infer R ?
R :
never
);
/*
Kind of sad this is `never` and not `number`.
If we really are talking overloads here,
then shouldn't ReturnType_1<> try to find the
overload that matches the type `(x : 1) => any`?
*/
//never
type rt_1 = ReturnType_1<typeof foo>;
type ReturnType_2<T> = (
T extends (x: 2) => infer R ?
R :
never
);
//string
type rt_2 = ReturnType_2<typeof foo>; |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Wait, what about this? Surely it's worth considering =/ It would be nice if |
TypeScript Version: TS Playground version (3.4.1), 3.5.0-dev.20190523
Search Terms: ReturnType, function, intersection
Code
Expected behavior:
Since function intersections are implemented as overloads,
always return the first return type.
Actual behavior:
Inconsistent return type.
Playground Link: Playground
Related Issues:
ReturnType<>
picking the return type of only one of the functions.The text was updated successfully, but these errors were encountered: