element implicitly has an 'any' type because expression of type 'string' can't be used to index in angularjs

1k Views Asked by At

***Elements implicitly has 'any' type because expression of type 'string' can't be used to indextype '{}' No index signature with a parameter type 'string' was find on type '{}'

And also showing object is possibly undefined.


I tried to implement this code in a typescript file but it is showing these error. Why? I am not able to find the error. please help.

getGlobalData() {
    return this.http.get(this.globalDataUrl, { responseType: 'text' }).pipe(
      map(result => {
        let data: GlobalDataSummary[] = [];
        let raw = {}
        let rows = result.split('\n');
        rows.splice(0, 1);
        // console.log(rows);
        rows.forEach(row => {
          let cols = row.split(/,(?=\S)/)

          let cs = {
            country: cols[3],
            confirmed: +cols[7],
            deaths: +cols[8],
            recovered: +cols[9],
            active: +cols[10],
          };
          let temp: GlobalDataSummary = raw[cs.country];
          if (temp) {
            temp.active = cs.active + temp.active
            temp.confirmed = cs.confirmed + temp.confirmed
            temp.deaths = cs.deaths + temp.deaths
            temp.recovered = cs.recovered + temp.recovered

            raw[cs.country] = temp;
          } else {
            raw[cs.country] = cs;
          }
        })
        return <GlobalDataSummary[]>Object.values(raw);
      })
    )
  }
1

There are 1 best solutions below

1
On

expression of type 'string' can't be used to indextype '{}'

That means you cannot do {}['somePropertyName']. And that is because your raw variable has no explicit type and your code initialised it as {}, an empty object, which has no property indexer.

You can give raw a type that allows your code to get its property via a string index like so:

let raw: {
    [key: string]: any
};