【发布时间】:2017-05-23 11:36:52
【问题描述】:
假设我有以下代码:
const results = //some complex datastructure generated by a third party api call
const reducedList = results.map((item) => item.awesome_key)
.map((awesomeKeyList) => awesomeKeyList
.reduce((memo, awesomeKey) => {//stuff},{})))
这段代码就像一个魅力。现在说我决定通过 pluck 将 Ramda 用于第一张地图,如下所示:
import R from Ramda;
R.pluck('awesome_key', results)
.map((awesomeKeyList) => awesomeKeyList
.reduce((memo, awesomeKey) => {},{})))
这将失败:
Property 'reduce' does not exist on type '{}'.
Ramda.pluck 上的类型有:
pluck<T>(p: string|number, list: any[]): T[];
pluck(p: string|number): <T>(list: any[]) => T[];
那些类型会阻止我以这种方式使用 reduce?
一个示例(简化)结构:
things: [
{
awesome_key: [{
long_name: 'string',
short_name: 'string',
types: {
0: 'string from set',
1?: 'string'
}
}]
other_fields not relevant here
}
]
【问题讨论】:
-
您能否分享一些 (simplified) 数据来证明该问题?
-
按要求添加来自第三方 API 的数据
-
无论哪种格式,我都得到相同的结果。因此,您的代码或数据可能有一些特定的东西。
-
你对比
results.map((item) => item.awesome_key)和R.pluck('awesome_key', results)的结果了吗,应该是一样的...
标签: typescript types ramda.js pluck