输入' {}'不能用作索引类型

时间:2017-08-24 11:08:14

标签: javascript typescript ramda.js

我正在使用ramda的<div class="errorpostcode test"> 函数。

toPairs

import { toPairs } from 'ramda'; renderMenuItems({ copy, remove, add, update }) { return ( toPairs({ copy, remove, add, update }) // make sure we have a function first. .filter(([, val]) => typeof val === 'function') .map(([key, value], idx) => <MenuItem key={idx} icon={ICONS[key]} //Error: Type '{}' cannot be used as an index type caption={CAPTIONS[key]} onClick={() => value()} />, ) ); } key位于value类型中。但根据文档(http://ramdajs.com/docs/#toPairs),它是一个字符串。 任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

感谢帮助我。

我可以通过设置已传递给函数key的对象的valuetoPairs类型来解决此问题。我在Github doc上找到了@ types.ramda测试用例(https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/ramda/ramda-tests.ts

这是解决方案。

 return (
      toPairs<string, number>({ copy, remove, add, update })
      ...

对于{}key

,默认类型为value