![](/img/trans.png)
[英]Element implicitly has an 'any' type because expression of type 'string' can't be used to index type - TypeScript Error
[英]Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
我有一个数组(iotDatas):
interface iotData {
id: string;
device_id: string;
color: number;
ph: number
created_at: string;
}
在 'color' 和 'ph' 属性之间进行选择的状态,默认值为 'color':
const [selecetedPropriety, setSelectedPropriety] = useState('color');
并且此地图返回打字错误:
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'
我不知道该怎么办。
谢谢你的帮助!
所以这里的第一件事是iotData
不是一个数组,它是一个对象,你试图使用这个对象的键来访问它。 我相信你可以在这里as keyof
。 比如下面,它指定字符串是iotData
一个键
iotData[selecetedPropriety as keyof iotData]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.