Javascript 如何在组合函数中传递泛型,而不是简单类型(例如:字符串、数字)

Javascript 如何在组合函数中传递泛型,而不是简单类型(例如:字符串、数字),javascript,typescript,composition,higher-order-functions,currying,Javascript,Typescript,Composition,Higher Order Functions,Currying,我有以下功能。它由许多函数组成,从右到左。就这样,很有魅力。我想使用泛型而不是标准类型(例如:字符串、数字等) 怎么做?有什么想法吗?我试过多种语法,但TS抱怨。无法在网上找到参考资料。谢谢您..将通用返回类型添加到减速器功能中: const composeUtility = <R>(...functions: ((acc: R) => R)[]) => (initialValue: R) => functions.reduceRight((accumula

我有以下功能。它由许多函数组成,从右到左。就这样,很有魅力。我想使用泛型而不是标准类型(例如:字符串、数字等)


怎么做?有什么想法吗?我试过多种语法,但TS抱怨。无法在网上找到参考资料。谢谢您..

将通用返回类型添加到减速器功能中:

const composeUtility = <R>(...functions: ((acc: R) => R)[]) => (initialValue: R) =>
    functions.reduceRight((accumulator, currentFn: (acc: R) => R) => currentFn(accumulator), initialValue);

const composeUtility=(…函数:((acc:R)=>R)[])=>(初始值:R)=>
函数.reduceRight((累加器,电流FN:(acc:R)=>R)=>currentFn(累加器,初始值);
所有减速器将接受相同类型的蓄能器
R

const withComposition = composeUtility(repeatFn, exclaimFn, screamFn);
console.log(withComposition('I Love TS - So Much')); // This only accepts strings. I want to pass more than that.
const composeUtility = <R>(...functions: ((acc: R) => R)[]) => (initialValue: R) =>
    functions.reduceRight((accumulator, currentFn: (acc: R) => R) => currentFn(accumulator), initialValue);