gpt4 book ai didi

reactjs - Ramda 或者不是 Ramda

转载 作者:行者123 更新时间:2023-12-05 03:42:33 29 4
gpt4 key购买 nike

<分区>

在我们的团队中,我们试图决定是否应该在我们的项目中使用 ramda。项目在Typesctipt上,前面是React,后面是Nestjs。

目前的优缺点列表是这样的

优点:

  1. 代码易于阅读
  2. 纯函数易于测试和重构

缺点:

  1. 难以阅读,如果你是 ramda 的新手
  2. Ramda 在类型方面存在问题,例如
interface Obj {p1: {p2: number}}

const obj: Obj = {
p1: {
p2: 1,
}
}

const value = pathOr(null, ['p1', 'p2'], obj); //type of "value" will be "any"
  1. 对类使用 ramda 看起来很尴尬,例如
class Cl {
method1() {...}

method2() {...}

method3() {
return compose(
this.method1.bind(this),
this.method2.bind(this),
)()
}
}

将很高兴看到对这些点的任何考虑,或添加更多的利弊。谢谢。

更新:

考虑到 geoffrey 的评论,当不可能不使用类时(比如在 NestJS 中),最好将 fp 风格的函数从类方法中分离出来,因为它们无论如何都是纯函数:

const method1Fn = () => {...};
const method2Fn = () => {...};
const method3Fn = () => compose(
method1Fn,
method2Fn,
)

class Cl {
method3() {
return method3Fn()
}
}

甚至

class Cl {
method3 = method3Fn
}

但在这种情况下,将为 Cl 的每个实例创建 method3。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com