首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型记录函数式推理

类型记录函数式推理
EN

Stack Overflow用户
提问于 2018-11-07 00:56:12
回答 1查看 64关注 0票数 0

我编写了这个简单的合成函数,它工作得很好。然而,为了确保类型安全,我不得不使用泛型来向编译器提供类型提示,尽管可以很容易地推断出"upperCaseAndLog“的签名。

代码语言:javascript
复制
const compose = <T, R>(...fns: Array<(a: any) => any>) => (a: T): R =>
  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<string, void>(
  greet,
  toUpperCase,
  log
);

upperCaseAndLog("bill");

我是不是错过了什么,有没有更优雅的方法来实现同样的目标?我假设像F#或Haskell这样的语言可以在没有任何额外信息的情况下推断类型。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-07 07:01:13

类型记录不能推断这样的链接类型(链接的意思是函数的参数依赖于前一个函数的结果)。

您甚至不能以足够通用的方式定义compose的签名,以至于它可以用于许多函数。我们可以做的是定义可以接受给定数量的函数的重载:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53182284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档