最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object.keys iteration causing Typescript error "Element implicitly has an 'any' type becau

programmeradmin3浏览0评论

I am trying to display the results of an API call that returns an object. In order to do a .map, I used Object.keys so that it will display the results.

I'm learning new to Typescript and attempting to practice in a personal project, but I'm stumped by this problem.

I am making an API call and getting back an object. In order to print the values, I'm using Object.keys to map over the values.

rates: Object
EUR: 0.8157272208
AUD: 1.3138918346
BRL: 5.1119993474
GBP: 0.7409087201
base: "USD"
date: "2020-12-18"

Here is my codesandbox (if I switch to a .ts file the whole thing blows up) =/src/index.js

TypeScript error in /Users/username/gitrepos/crypto-convert/src/App.tsx(60,39):
Element implicitly has an 'any' type because index expression is not of type 'number'.  TS7015

    58 |               Object.keys(cryptos).map((crypto, index) => (
    59 |                 <li key={index}>
  > 60 |                   {crypto} : {cryptos[crypto]}
       |                                        ^
    61 |                 </li>

There are some funky solutions online and ways to get around the error, but I don't want to do anything hacky as the point is to learn more about TS. Can anyone help me figure out what I've done wrong here? Thanks!

I am trying to display the results of an API call that returns an object. In order to do a .map, I used Object.keys so that it will display the results.

I'm learning new to Typescript and attempting to practice in a personal project, but I'm stumped by this problem.

I am making an API call and getting back an object. In order to print the values, I'm using Object.keys to map over the values.

rates: Object
EUR: 0.8157272208
AUD: 1.3138918346
BRL: 5.1119993474
GBP: 0.7409087201
base: "USD"
date: "2020-12-18"

Here is my codesandbox (if I switch to a .ts file the whole thing blows up) https://codesandbox.io/s/challenge-7-fetch-a-list-final-forked-7wtwu?file=/src/index.js

TypeScript error in /Users/username/gitrepos/crypto-convert/src/App.tsx(60,39):
Element implicitly has an 'any' type because index expression is not of type 'number'.  TS7015

    58 |               Object.keys(cryptos).map((crypto, index) => (
    59 |                 <li key={index}>
  > 60 |                   {crypto} : {cryptos[crypto]}
       |                                        ^
    61 |                 </li>

There are some funky solutions online and ways to get around the error, but I don't want to do anything hacky as the point is to learn more about TS. Can anyone help me figure out what I've done wrong here? Thanks!

Share Improve this question edited Dec 20, 2020 at 18:13 stonerose036 asked Dec 20, 2020 at 18:06 stonerose036stonerose036 2814 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

Object.keys is badly typed and returns string[] instead of the key of the argument. Thus, crypto cannot be ensured to be a key of cryptos. One way to circumvent this is to cast Object.keys:

(Object.keys(cryptos) as keyof typeof cryptos).map(...)

Or, to just use Object.entries to iterate over both keys and values:

Object.entries(cryptos).map(([key, value], index) => (
    <li key={index}>
        {key}: {value}
    </li>
)
发布评论

评论列表(0)

  1. 暂无评论