简体   繁体   中英

Type '{}' cannot be used as an index type

I am using ramda's toPairs functions.

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()}
          />,
      )
    );
  }

The key and value is in type {} . But as per docs( http://ramdajs.com/docs/#toPairs ) it is a string. Can any one help me to solve this?

thanks to helping me.

I am able to solve the issue by setting the type of key and value of object that I have passed to the function toPairs . I found it on Github doc for @types.ramda test cases ( https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/ramda/ramda-tests.ts )

Here is the solution.

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

It looks like the default type is {} for both the key and the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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