Consider the following overloaded function:
function foo(arg1: string, cb: (err: Error|null, res: string) => void): void
function foo(arg1: string, arg2: string, cb: (err: Error|null, res: string) => void): void
I want promisify
to work with such functions. But the default implementation returns an invalid type.
As it returns
(arg1: string, arg2: string) => Promise<{}>
I'd expect it to return
{
(arg1: string): Promise<string>;
(arg1: string, arg2: string): Promise<string>;
}
Considering this, I'd like to fix the typings. I managed to override that specific prototype using the following :
export type Callback<T> = (err: Error | null, reply: T) => void;
export type Promisify<T> =
T extends {
(arg1: infer T1, cb?: Callback<infer U>): void;
(arg1: infer P1, arg2: infer P2, cb?: Callback<infer U2>): void;
} ? {
(arg1: T1): Promise<U>;
(arg1: P1, arg2: P2): Promise<U2>;
} :
T extends (cb?: Callback<infer U>) => void ? () => Promise<U> :
T extends (arg1: infer T1, cb?: Callback<infer P>) => void ? (arg1: T1) => Promise<P> :
T extends (arg1: infer T1, arg2: infer T2, cb?: Callback<infer U>) => void ? (arg1: T1, arg2: T2) => Promise<U> :
T extends (arg1: infer T1, arg2: infer T2, arg3: infer T3, cb?: Callback<infer U>) => void ? (arg1: T1, arg2: T2, arg3: T3) => Promise<U> :
T extends (arg1: infer T1, arg2: infer T2, arg3: infer T3, arg4: infer T4, cb?: Callback<infer U>) => void ? (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<U> :
T;
But it requires me to specifically list all potential method overloads.
It there a way transform all methods overloads at once, similarily to how we can transform object properties?