UNPKG

825 BTypeScriptView Raw
1// Since TS 3.6 the checker knows that the correct type of returned values and yielded values https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-6.html
2// Generator<T, TReturn, TNext> => TReturn
3// Function => ReturnType<Function>
4// others => others
5type ExtractType<I> = I extends { [Symbol.iterator]: () => Iterator<any, infer TReturn, any> } ? TReturn
6 : I extends (...args: any[]) => any ? ReturnType<I>
7 : I;
8
9interface Co {
10 <F extends (...args: any[]) => Iterator<any, any, any>>(fn: F, ...args: Parameters<F>): Promise<
11 ExtractType<ReturnType<F>>
12 >;
13 default: Co;
14 co: Co;
15 wrap: <F extends (...args: any[]) => Iterator<any, any, any>>(
16 fn: F,
17 ) => (...args: Parameters<F>) => Promise<ExtractType<ReturnType<F>>>;
18}
19
20declare const co: Co;
21
22export = co;