-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.d.ts
57 lines (48 loc) · 2.02 KB
/
test.d.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
declare function createFnChain<T extends AnyFunc[]>(...fn: PipeArgs2<T> extends T ? T : PipeArgs2<T> ): LastFunctionReturnType<T>;
type LastFunctionReturnType<T extends AnyFunc[]> =
T extends [...infer N extends AnyFunc[], (arg: infer A) => infer B]
? B extends Promise<any>
? B
: LastFunctionReturnType<N> extends Promise<any>
? Promise<B>
: B
: T extends [(...args: infer A) => infer B]
? B
: T extends [(arg: infer A) => infer B]
? B
: never;
type AnyFunc = (...arg: any) => any;
type PipeArgs<F extends AnyFunc[], Acc extends AnyFunc[] = []> = F extends [
(...args: infer A) => infer B
]
? [...Acc, (...args: A) => B]
: F extends [(arg: infer A) => infer B]
? [...Acc, (arg: A) => B]
: F extends [(...args: infer A) => infer B, ...infer Tail]
? Tail extends [(arg: B) => infer C, ...any[]]
? PipeArgs<Tail, [(...args: A) => B, ...Acc]>
: Acc
: Acc;
type M = PipeArgs<[(arg: string) => number, () => string]>
type PipeArgs2<F extends AnyFunc[], Acc extends AnyFunc[] = []> =
F extends [(...args: infer A) => infer B]
? Acc extends []
? [(...args: A) => B]
: [...Acc, (arg: A[0]) => B]
: F extends [(...args: infer A) => infer B, ...infer Tail]
? Tail extends [(arg: infer C) => any, ...any[]]
? B extends Promise<infer D>
? C extends D
? D extends C
? PipeArgs2<Tail, [(...args: A) => B, ...Acc]>
: PipeArgs2<[(...args: A) => B], Acc>
: PipeArgs2<[(...args: A) => B], Acc>
: C extends B
? B extends C
? PipeArgs2<Tail, [(...args: A) => B, ...Acc]>
: PipeArgs2<[(...args: A) => B], Acc>
: PipeArgs2<[(...args: A) => B], Acc>
: PipeArgs2<[(...args: A) => B], Acc>
: Acc;
type S = PipeArgs2<[(arg: string) => number, (arg: number) => Promise<string>]>
type NNN = [() => string] extends [(args: string) => infer B, ...infer Tail] ? B : never;