在typescript
中, 如何定义类型Compose
使得
type F = (arg: B) => C;
type G = (arg: A) => B;
// Compose === (f: F, g: G) => (arg: A) => C
在typescript的推断类型更新之后这个问题有了较为简明的解决方法
type Morph<A = any, R = any> = (arg: A) => R
type RetOf<F extends Morph> = F extends Morph<any, infer R> ? R : void
type ArgOf<F extends Morph> = F extends Morph<infer Arg, any> ? Arg : void
type Compose = <F extends Morph, Arg>(f: F, g: Morph<Arg, ArgOf<F>>) => Morph<Arg, RetOf<F>>