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
A type util to produce an alternation of function overloads. #868
Comments
GPT4 plus testing shows to me that It appears possible to do this via type OverloadParams<T> = T extends {
(...args: infer A): any;
(...args: infer B): any;
} ? A | B : never; A general solution working for N overloads seems elusive. In practical terms we only ever need this for up to say 5 overloads. So unrolling it seems practical. A recursive implementation would be nice, but may lead to longer compilation time. This definitely seems to work for me: type OverloadParams<T> = T extends {
(...args: infer A);
(...args: infer B);
(...args: infer C);
(...args: infer D);
(...args: infer E);
} ? A | B | C | D | E : T extends {
(...args: infer A);
(...args: infer B);
(...args: infer C);
(...args: infer D);
} ? A | B | C | D : T extends {
(...args: infer A);
(...args: infer B);
(...args: infer C);
} ? A | B | C : T extends {
(...args: infer A);
(...args: infer B);
} ? A | B : never; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example situation:
Type Z here resolves to
string[]
, notstring | string[]
, because overloads just get the last declared one matched.I am curious to know if this is possible to implement. As far as I can tell this also affects the standard function overload approach, e.g.:
To be clear, what I'm asking for is a util e.g.
OverloadParameters
which in this case would produce for typeOverloadParameters<F>
a value of[string] | [string[]]
.I'm actually currently unsure of how to unwrap the parameter-array situation there but I'm sure it's manageable.As expected, the type([string] | [string[]])[0]
isstring | string[]
.The text was updated successfully, but these errors were encountered: