繁体   English   中英

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM