Reactjs 使用React>;元素隐式地具有一个';任何';类型,因为表达式的类型为';字符串';can';不可用于索引
在尝试使用带有react的typescript构建项目时,我被这个问题困住了。 下面是来自typescript的错误消息Reactjs 使用React>;元素隐式地具有一个';任何';类型,因为表达式的类型为';字符串';can';不可用于索引,reactjs,typescript,Reactjs,Typescript,在尝试使用带有react的typescript构建项目时,我被这个问题困住了。 下面是来自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;
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 | })`
似乎错误发生在带有参数ele的内联22中
假设我有这个:
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;
声明全局{
类型Dictionary={[key:string]:T};
}
让newObj:Dictionary={};
Data.dataset.forEach(ele=>{
常量键=对象键(ele);
keys.forEach(e=>{
//console.log
如果(!newObj[e]){
newObj[e]=[];
}
newObj[e].push(ele[e]);
})
});
常量选项:高图表。选项={
标题:{
文本:“我的图表”
},
亚克斯:{
标题:{
文字:“婴儿潮”
}
},
系列:[{
键入:“行”,
姓名:'a',
数据:[1,2,3]
}, {
键入:“行”,
姓名:'b',
数据:[11,22,3]
},{
键入:“行”,
姓名:'c',
数据:[1,23,53]
}],
}
常量应用=(道具:HighchartsReact.props)=>
导出默认应用程序;
我试着在TypeScript文档上到处寻找,但我该如何连接它,以便它可以接受ele[e]?问题似乎是您试图使用any类型的字符串作为索引
newObj[e].push(ele[e]);
Data.dataset在将forEach“ele”作为字符串类型的键进行迭代以及循环使用number类型的键时,是一个{}
字典。正在引发异常,因为您试图使用字符串作为索引注意如果Datadataset
中的键是string类型,则forEach循环将以string(即子字符串)打印每个char
。如果你想把字典复制到新字典里。可以通过执行以下操作来实现
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] = [];
}
}
let copy\u db:Dictionary={};
//循环对象中的所有关键点
for(让prop进入Data.dataset){
//确保对象具有此值,而不是其原型
if(Data.dataset.hasOwnProperty(prop)){
//为新词典增添价值
复制_db[prop]=(Data.dataset[prop];
//如果您希望在新字典中有一个空值,那么可以添加一个具有copy_db[prop]=[]的else;
}
}