How do i solve typescript error No index signature with a parameter of type 'string' was found on type 'Object'

Viewed 29

I wrote a typescript function for a service to return an observable in angular and I encountered the error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'. No index signature with a parameter of type 'string' was found on type 'Object'.

How do I solve it. Below is the link to my code.

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { IProperty } from '../property/IProperty.interface';

@Injectable({
  providedIn: 'root'
})
export class HousingService {

  constructor(private http: HttpClient) { }

  //method for getting all the realEstate properties
  getAllProperties(): Observable<IProperty[]> {
    return this.http.get('../../data/properties.json').pipe(
      map(data => {
        const propertiesArray: Array<IProperty> = [];
        for (const id in data) {
          if (data.hasOwnProperty(id)) {
            propertiesArray.push(data[id]);
          }
        }
        return propertiesArray;
      })
    );
  }
}

1 Answers

http.get is generic and lets you pass in the expected type of data, so you could use { [key: string]: string } as the type:

return this.http.get<{ [key: string]: string }>('../../data/properties.json').pipe(
Related