
[英]Typescript error : No index signature with a parameter of type 'string' was found on type '{}'
[英]How do i solve typescript error No index signature with a parameter of type 'string' was found on type 'Object'
我写了一个 typescript function 的服务以返回 angular 中的可观察对象,我遇到了错误元素隐含地有一个“任何”类型用于索引类型“对象”的表达式。 在“对象”类型上找不到具有“字符串”类型参数的索引签名。
我该如何解决。 下面是我的代码的链接。
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;
})
);
}
}
http.get
是通用的,可让您传入预期的数据类型,因此您可以使用{ [key: string]: string }
作为类型:
return this.http.get<{ [key: string]: string }>('../../data/properties.json').pipe(
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.