Hi,
I am trying to run the below code but it is giving me the error in the subject of this message.
element implicitly has an 'any' type because expression of type 'string' can't be used to index type
HousingService File:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { IProperty } from 'src/app/property/IProperty.interface';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class HousingService {
constructor(private http:HttpClient) { }
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;
})
)
}
}
error is line where I am trying to add to array (push).
this is the interface:
export interface IProperty{
Id: number;
Name: string;
Type: string;
Price: number;
}
and this is the component:
import { Component, OnInit } from '@angular/core';
import { HousingService } from 'src/app/services/housing.service';
import { IProperty } from 'src/app/property/IProperty.interface';
@Component({
selector: 'app-property-list',
templateUrl: './property-list.component.html',
styleUrls: ['./property-list.component.css']
})
export class PropertyListComponent implements OnInit{
properties: Array<IProperty>=[];
constructor(private housingService: HousingService) {}
ngOnInit(): void{
this.housingService.getAllProperties().subscribe(
data=> {
this.properties=data;
}, error => {
console.log(error);
}
);
}
}