簡體   English   中英

如何解決 typescript 錯誤在“對象”類型上找不到帶有“字符串”類型參數的索引簽名

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM