繁体   English   中英

类型“ {}”不能用作索引类型

[英]Type '{}' cannot be used as an index type

我正在使用ramda的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()}
          />,
      )
    );
  }

keyvalue的类型为{} 但是根据docs( http://ramdajs.com/docs/#toPairs ),它是一个字符串。 谁能帮我解决这个问题?

感谢帮助我。

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

这是解决方案。

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

看起来keyvalue的默认类型均为{}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM