-
Notifications
You must be signed in to change notification settings - Fork 19
/
functions.ts
executable file
·64 lines (63 loc) · 1.71 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Given a type of object with methods, make some (or all) of the return values
* "async" (i.e., returning a `string` becomes returning a `Promise<string>`).
*
* All non-function properties are excluded from the resultant type
*
* @public
* @example
* ```ts
*
* interface User {
* isAdmin: boolean; // will not be included
* login(): boolean;
* resetPassword(): string;
* sendEmail(body: string): boolean;
* }
* const x: AsyncMethodReturns<User> ...; // {
* // login(): Promise<boolean>,
* // resetPassword(): Promise<string>
* // }
* ```
*
*/
export type AsyncMethodReturns<T, K extends keyof T = keyof T> = {
[KK in K]: T[KK] extends (...args: any[]) => PromiseLike<any>
? T[KK]
: T[KK] extends (...args: infer A) => infer R
? (...args: A) => Promise<R>
: T[KK];
};
/**
* Extract the arguments from a function type, and emit them as a tuple
* @public
*
* @remarks
* Supports up to five arguments, otherwise fails via emitting a `never`
*
* @example
* ```ts
* function foo(a: string, b: number): void { }
* type FooArgs = ExtractArgs<typeof foo>; // [string, number]
* type FooFirstArg = FooArgs[0] // string
* ```
*/
export declare type ExtractArgs<F> = F extends (
a: infer A,
b: infer B,
c: infer C,
d: infer D,
e: infer E
) => any
? [A, B, C, D, E]
: F extends (a: infer A, b: infer B, c: infer C, d: infer D) => any
? [A, B, C, D]
: F extends (a: infer A, b: infer B, c: infer C) => any
? [A, B, C]
: F extends (a: infer A, b: infer B) => any
? [A, B]
: F extends (a: infer A) => any
? [A]
: F extends () => any
? []
: never;