[英]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.