简体   繁体   English

带有 React > Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引

[英]Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index

I'm stuck on this issue whilst trying to build a project using typescript with react.我在尝试使用带有 react 的 typescript 构建项目时遇到了这个问题。 Here are the error messages from typescript.这是来自打字稿的错误消息。

Enter code hereElement implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ time: string; EC_slab1: number; EC_slab2: number; EC_drain_PC: number; WC_slab1: number; WC_slab2: number; CO2air: number; HumDef: number; Rhair: number; Tair: number; EnScr: number; BlackScr: number; ... 4 more ...; Tout: number; }'.
  No index signature with a parameter of type 'string' was found on type '{ time: string; EC_slab1: number; EC_slab2: number; EC_drain_PC: number; WC_slab1: number; WC_slab2: number; CO2air: number; HumDef: number; Rhair: number; Tair: number; EnScr: number; BlackScr: number; ... 4 more ...; Tout: number; }'.  TS7053

    20 |       newObj[e] = [];
    21 |     }
  > 22 |     newObj[e].push(ele[e]);
       |                    ^
    23 |     console.log(ele[e]);
    24 |     console.log(e);
    25 |   })`

Seems that the errors occur inline 22 with parameter ele.似乎错误发生在带有参数 ele 的内联 22。

Let's say I have this:假设我有这个:

declare global {
  type Dictionary<T> = { [key: string]: T };
}

let newObj: Dictionary<any> = {};
Data.dataset.forEach(ele => {
  const keys = Object.keys(ele);
  keys.forEach(e => {
    // console.log
    if(!newObj[e]){
      newObj[e] = [];
    }
    newObj[e].push(ele[e]);
  })
});

const options: Highcharts.Options = {
  title: {
      text: 'My chart'
  },
  yAxis: {
    title: {
        text: 'Baby boom'
    }
},
  series: [{
      type: 'line',
      name:'a',
      data: [1, 2, 3]
  }, {
    type: 'line',
    name:'b',
    data: [11, 22, 3]
},{
  type: 'line',
  name:'c',
  data: [1, 23, 53]
}],
  
}

const App = (props: HighchartsReact.Props) => <div>
    <HighchartsReact
        highcharts={Highcharts}
        options={options}
        {...props}
    />
</div>

export default App;

I tried looking everywhere on TypeScript docs, but how the heck am I suppose to interface this so it can accept ele[e]??我试着在 TypeScript 文档上到处寻找,但我怎么想怎么接口它才能接受 ele[e]??

It looks like the problem is that your trying to use a string of type any as an index.看起来问题在于您尝试使用类型为 any 的字符串作为索引。

newObj[e].push(ele[e]); 

Data.dataset being a {} dictionary when iterating forEach "ele" as key of type string and when looping keys of type number. Data.dataset 是一个{}字典,当将 forEach "ele" 作为字符串类型的键进行迭代以及循环类型为 number 的键时。 The exception is being thrown because your trying to use a string as an index.抛出异常是因为您尝试使用字符串作为索引。 NOTE if the keys in Datadataset are of type string, the forEach loop will print each char in string ie, substring.注意如果Datadataset中的键是字符串类型,则 forEach 循环将打印字符串中的每个char ,即子字符串。 If you are trying to copy the dictionary into a new dictionary.如果您尝试将字典复制到新字典中。 It can be achieved doing the following.可以通过以下方式实现。

let copy_db: Dictionary<any> = {};

// Loop over all the keys in the object
for (let prop in Data.dataset) {

 // Make sure the object has this value, and not its prototype
 if (Data.dataset.hasOwnProperty(prop)) {
    // throws value into new dictionary
    copy_db[prop] = (Data.dataset[prop];
    // if you would like an empty value in the new dictionary, then you would add a else with copy_db[prop] = [];
  }
}

暂无
暂无

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

相关问题 React typescript - 元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引类型 - React typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 React Typescript 中索引类型 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type in React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript 错误 - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - TypeScript Error Typescript 错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript error : Element implicitly has an 'any' type because expression of type 'string' can't be used to index TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TypeScript 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 反应 typescript 错误 - 元素隐式具有任何类型,因为类型字符串的表达式不能用于索引类型 {} - react typescript error - element implicitly has an any type because expression of type string cant be used to index type {} TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}” - TypeScript - Element implicitly has an 'any' type because expression of type '“storyFile”' can't be used to index type '{}' 元素隐式具有 &#39;any&#39; 类型,因为类型 &#39;any&#39; 的表达式不能用于索引类型 &#39;{}&#39; - React Anagram - Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM