简体   繁体   English

map() 中的打字稿错误:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型

[英]Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type

i have an array (iotDatas) of:我有一个数组(iotDatas):

interface iotData {
 id: string;
 device_id: string;
 color: number;
 ph: number
 created_at: string;
} 

a state to select between the 'color' and 'ph' properties, with a default value 'color':在 'color' 和 'ph' 属性之间进行选择的状态,默认值为 'color':

const [selecetedPropriety, setSelectedPropriety] = useState('color');

and this map return a typescrip error:并且此地图返回打字错误:

const yesterdayData = iotDatas.map(iotDada => {
 return { x: iotDada.created_at, y: iotDada[selecetedPropriety]};
});

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ioTData'.
No index signature with a parameter of type 'string' was found on type 'ioTData'

i dont know what to do.我不知道该怎么办。

Thans for your help!谢谢你的帮助!

So the first thing here is that iotData is not an array, it is an object, which you are trying to access using a key of this object.所以这里的第一件事是iotData不是一个数组,它是一个对象,你试图使用这个对象的键来访问它。 I believe you can use as keyof here.我相信你可以在这里as keyof Such as below, which specifies that the string is a key of iotData比如下面,它指定字符串是iotData一个键

iotData[selecetedPropriety as keyof iotData]

暂无
暂无

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

相关问题 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - 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 错误元素隐式具有“任何”类型,因为“任何”类型的表达式不能用于索引类型 - TypeScript error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 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 带有 React > Element 的 Typescript 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引 - Typescript with React > 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 错误 - 元素隐式具有任何类型,因为类型字符串的表达式不能用于索引类型 {} - 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 '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM