在“对象"类型上找不到带有“字符串"类型参数的索引签名 [英] No index signature with a parameter of type 'string' was found on type 'Object'

查看:198
本文介绍了在“对象"类型上找不到带有“字符串"类型参数的索引签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**有人能告诉我这个问题的解决方案吗?我收到错误,请帮助它给我错误,例如 Element 隐式具有任何"类型,因为字符串"类型的表达式不能用于索引对象"类型 **

**Can someone tell me the solution to this problem? I am getting error please help Its giving me error like Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object' **

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {map} from 'rxjs/operators';
import { IProperty } from '../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;
  })
);
}
}

推荐答案

从您的代码来看,您基本上是从 JSON 属性中获取所有值.您收到该错误的原因是数据具有 {} 类型,因为您没有在泛型 get<T = {}>() 中指定它方法.

Judging by your code you are basically getting all the values from properties JSON. The reason you are getting that error is because the data has as type {}, because you do not specify it in the generic get<T = {}>() method.

稍微改写会导致这个:

getAllProperties(): Observable<IProperty[]>{
  return this.http.get<Record<string, IProperty>>('data/properties.json').pipe(
    map(data => Object.values(data));
  };
}

你会很高兴的.

这篇关于在“对象"类型上找不到带有“字符串"类型参数的索引签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆