-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Function argument inference only handles one overload #26591
Comments
See comments in #21847 - overload resolution in conditional types is not supported yet |
I also encountered this issue while working with a partially-applied version of a hyperscript-style function. I was able to work around it by specifying the last overload of the
This may not help in all cases where this is an issue, but I thought I'd share just in case others might find this useful. |
I was hoping to leverage the existing typing on known event names in common node I took the the following approach in the playground and that led me to adding in my use-case to this issue. interface EE {
on(event: string, handler: () => void): void;
}
interface Request extends EE {
on(foo: 'error', handler: (error: Error) => void): void;
on(foo: 'data', handler: (data: string) => void): void;
}
type HandlerForEvent<T extends EE, E extends string> = T extends { on: (event: E, handler: infer H) => void } ? H : never;
type ErrorHandler = HandlerForEvent<Request, 'error'>;
type ErrorHandlerArgs = Parameters<ErrorHandler>;
// Types as never
type DataHandler = HandlerForEvent<Request, 'data'>;
type DataHandlerArgs = Parameters<DataHandler>;
// Types as [string] !! If you reverse the order of the It would be neat if the arguments for overloaded methods were treated as a union of tuples according to the overloads. |
well, i have an idea type GetArgumentsType<Delay> =
| (Delay extends (one: infer ONE) => any ? [ONE] : never)
| (Delay extends (...args: infer MANY) => any ? MANY : never); this worked for me in some case for infer arguments |
@RyanCavanaugh Is this closed as "won't fix" or was the issue resolved? |
Design limitation means "No apparent way of fixing this exists (short of wholescale redesign)". "Won't Fix" would mean "We could fix it but choose not to". |
I see. Thanks for clarifying! 👌🏼 |
TypeScript Version: 3.1.0-dev.20180821
Search Terms: function argument infer overload return type rest tuple
Code
Expected behavior:
Type
A
is a union of the return types of every overload, or an error along the lines of "cannot infer things about an overloaded function" is generated.Actual behavior:
Type
A
isobject
.This also happens when inferring function params:
Playground Link:
http://www.typescriptlang.org/play/#src=function%20foo%20(a%3A%20string)%3A%20string%3B%0D%0Afunction%20foo%20(a%3A%20number%2C%20b%3A%20string)%3A%20boolean%3B%0D%0Afunction%20foo%20(a%3A%20boolean%2C%20...args%3A%20object%5B%5D)%3A%20object%3B%0D%0Afunction%20foo%20(...args%3A%20any%5B%5D)%3A%20any%20%7B%0D%0A%7D%0D%0A%0D%0Atype%20A%20%3D%20ReturnType%3Ctypeof%20foo%3E%3B%0D%0A%0D%0Atype%20Parameters%3CT%20extends%20(...args%3A%20any%5B%5D)%20%3D%3E%20any%3E%20%3D%20T%20extends%20(...args%3A%20infer%20U)%20%3D%3E%20any%20%3F%20U%20%3A%20never%3B%0D%0A%0D%0Atype%20B%20%3D%20Parameters%3Ctypeof%20foo%3E%3B%20%2F%2F%20%5Bboolean%2C%20...object%5B%5D%5D
The text was updated successfully, but these errors were encountered: