Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

如何定义Compose的泛型类型

typescript中, 如何定义类型Compose使得

type F = (arg: B) => C;
type G = (arg: A) => B;

// Compose === (f: F, g: G) => (arg: A) => C

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

在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>>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...