【问题标题】:Type errors in Angular.ioAngular.io 中的类型错误
【发布时间】:2017-12-30 10:20:09
【问题描述】:

使用 Angular2/4 并开发服务,但我不确定为什么会不断出现这种类型的错误。我相信我正确设置了我的类型,但这显然是错误的......

错误:

ERROR in C:/Users/rutherfordc.AA/Documents/GitHub/Sweeper/src/app/people.service.ts (19,12): The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the t
ype arguments explicitly.
  Type argument candidate 'Response' is not a valid type argument because it is not a supertype of candidate 'Response'.
    Types of property 'type' are incompatible.
      Type 'string' is not assignable to type 'ResponseType'.

这是根据错误引发错误的代码。 (为完整起见,添加了额外的行。)

  person:Person = new Person();
  private dataRetrievalUrl: string = '/api/';
  constructor(private http: Http) { }
  public getPersonById(id: string):Observable<Person>{
    var fullUrl: string = this.dataRetrievalUrl + id;
    return this.http.get(fullUrl)
                    .map(this.extractData)
                    .catch(this.handleError);
  }

我错过了什么?

【问题讨论】:

  • Response 的分型版本存在冲突,很可能是由于导入了一些使用不同版本的 @angular/http 或类似内容的库。
  • 谢谢@torazaburo,完全忘记导入响应了!必须弄清楚这些。

标签: angular http typescript observable


【解决方案1】:

.catch(this.handleError); 中的 catch 正在更改返回类型,导致类型不匹配。因此出现错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2023-04-03
    • 2012-01-06
    • 2014-03-09
    • 2013-10-10
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多