Home > other >  Element implicitly has an 'any' type because type 'Map<String, DayConsumptionDto[]
Element implicitly has an 'any' type because type 'Map<String, DayConsumptionDto[]

Time:04-09

I'm stucked with this issue and I didn't find any solution even if there is a lot of question around this error, in my case I have a Map and I coudn't apply the solutions.

Here is my code :

I have to convert a map to send to the back-end like this :

const convMap = {} as Map<String, Array<DayConsumptionDto>>;
    map.forEach((val: Array<DayConsumptionDto>, key: string) => {
      convMap[key] = val;
    });

I'm getting this Error :

Element implicitly has an 'any' type because type 'Map<String, DayConsumptionDto[]>' has no index signature. Did you mean to call 'convMap.set'?

if I put const convMap = {} as any it works perfectly but I don't do that, can you please help me to fix it ?

CodePudding user response:

you can try like this

const convMap:{[key:string]:Array<DayConsumptionDto>}[] = []
    map.forEach((val: Array<DayConsumptionDto>, key: string) => {
      convMap[key] = val;
    });

this will create an array

or

const convMap= {} as {[key:string]:Array<DayConsumptionDto>};
    map.forEach((val: Array<DayConsumptionDto>, key: string) => {
      convMap[key] = val;
    });

this will create an object

  • Related