Commit 8d261b2 1 parent d5f1a70 commit 8d261b2 Copy full SHA for 8d261b2
File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { Func } from "./typing.ts" ;
2
+
3
+ type Identity < T > = ( x : T ) => T ;
4
+
5
+ type UnwrapPromise < T > = T extends Promise < infer U > ? U : T ;
6
+
7
+ type UnwrapPromiseFn < T extends Func > = (
8
+ ...args : Parameters < T >
9
+ ) => UnwrapPromise < ReturnType < T > > ;
10
+
11
+ type SimpleCompose < F , G > = G extends ( ...args : infer GArgs ) => infer GReturn
12
+ ? F extends ( x : UnwrapPromise < GReturn > ) => infer FReturn
13
+ ? ( ...args : GArgs ) => UnwrapPromise < FReturn >
14
+ : never
15
+ : never ;
16
+
17
+ type Compose < F , G > = F extends Identity < infer _ >
18
+ ? G extends Identity < infer _ > ? UnwrapPromiseFn < G > : SimpleCompose < F , G >
19
+ : SimpleCompose < F , G > ;
20
+
21
+ export type ComposeMany < Fs extends Func [ ] > = Fs extends [ ] ? never
22
+ : Fs extends [ infer F extends Func ] ? F
23
+ : Fs extends
24
+ [ infer F extends Func , infer G extends Func , ...infer rest extends Func [ ] ]
25
+ ? rest extends [ ] ? Compose < G , F >
26
+ : Compose < ComposeMany < rest > , Compose < G , F > >
27
+ : never ;
You can’t perform that action at this time.
0 commit comments