我有一个简单的服务,内容如下:
import { Injectable } from '@angular/core'; import { Http,Response } from '@angular/http'; import 'rxjs/add/observable/throw'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import { Observable } from 'rxjs/Observable'; @Injectable() export class AddressService { constructor(private http: Http) { } getAnything = (): Observable<Response> => { return this.http.get('https://my_api.com') .map(this.handleSuccess) .catch(this.handleError); } handleError = (error: Response): Observable<Response> => { return Observable.throw(error || 'Server Error'); } handleSuccess = (response: Response): Observable<Response> => { let body; if (response.text()) { body = response.json(); } return body || {}; } }
它工作得很好,直到我将Typescript从2.3.4升级到2.4.1.
Type 'Observable<Response | Observable<Response>>' is not assignable to type 'Observable<Response>'
这有什么意义? TS 2.4.x中的哪些更改使我的应用程序停止正常工作?
TypeScript 2.4引入了更好的泛型检查.它突出显示了应该修复的代码中的错误.
原文链接:https://www.f2er.com/angularjs/140247.html例如,handleSuccess的返回类型与返回的类型不匹配;它返回一个匿名对象,但输入为Observable< Response>.因为它与map一起使用,你最终会得到一个组合的observable,它被输入为Observable< Response |可观察到的与LT;响应>取代. 您看到的错误是真实的,应该修复.