简体   繁体   中英

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:

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':

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. I believe you can use as keyof here. Such as below, which specifies that the string is a key of iotData

iotData[selecetedPropriety as keyof iotData]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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