Created
October 9, 2018 11:07
-
-
Save flapenguin/454ac9ea48ef9aa24d807b10f3725108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Promisified<T extends Function> = | |
T extends (cb: (err: any, data: infer TData) => any) => void ? () => Promise<TData> : | |
T extends (arg0: infer TArg0, cb: (err: any, data: infer TData) => any) => void ? (arg0: TArg0) => Promise<TData> : | |
T extends (arg0: infer TArg0, arg1: infer TArg1, cb: (err: any, data: infer TData) => any) => void ? (arg0: TArg0, arg1: TArg1) => Promise<TData> : | |
/* etc */ | |
never; | |
function w0args(cb: (err: Error, data: string) => void) {} | |
function w1args(arg0: number, cb: (err: Error, data: string) => void) {} | |
function w2args(arg0: number, arg2: boolean, cb: (err: Error, data: string) => void) {} | |
type w0argsPromisified = Promisified<typeof w0args>; | |
type w1argsPromisified = Promisified<typeof w1args>; | |
type w2argsPromisified = Promisified<typeof w2args>; | |
type Source = { | |
w0args: typeof w0args; | |
w1args: typeof w1args; | |
w2args: typeof w2args; | |
}; | |
type PromisifiedSource = { | |
[TKey in keyof Source]: Promisified<Source[TKey]>; | |
}; | |
const x: PromisifiedSource = null as any; | |
const r0: Promise<string> = x.w0args(); | |
const r1: Promise<string> = x.w1args(42); | |
const r2: Promise<string> = x.w2args(42, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment