Typescript不能推断这样的链接类型(链接的意义是函数的参数依赖于上一个函数的结果)。
compose
type Fn<A, R> = (a: A) => R // just to be a bit shorter in the compose signature, you can use teh function signature directly
function compose<T, P1, P2, R>(fn1: Fn<T, P1>, fn2: Fn<P1, P2>, f3: Fn<P2, R>) : Fn<T, R>
function compose<T, P1, R>(fn1: Fn<T, P1>, f2: Fn<P1, R>) : Fn<T, R>
function compose(...fns: Array<(a: any) => any>) {
return function (a: any) {
return fns.reduce((b, f) => f(b), a);
}
}
const greet = (s: string) => "Hello " + s;
const toUpperCase = (s: string) => s.toUpperCase();
const log = console.log;
const upperCaseAndLog = compose(
greet,
toUpperCase,
log
);
upperCaseAndLog("bill");//(a: string) => void