rxjs:5.0.0-beta.2 打字稿:1.8 Angular2:beta-9
我正在尝试模拟一个 http 服务类,但在扩展类时遇到问题,如 angular 2 文档中所述。
似乎您无法覆盖返回类型化可观察对象的方法?我错过了什么?
import {Observable} from "rxjs/Observable";
import {IQuery} from "../Query";
import {Http} from "angular2/http";
export interface IQuery<TResult> {
}
export class A {
public test = <T>(q: IQuery<T>): Observable<T> => {
var queryName = this.getName(query);
var url = `api/query/${queryName}`;
return this.httpPost<TResult>(url, query);
};
}
export class AMock extends A {
public test = <T>(q: IQuery<T>): Observable<T> => {
return mockObservable;
};
}
Severity Code Description Project File Line Suppression State
Error TS2415 Class 'AMock' incorrectly extends base class 'A'.
Types of property 'test' are incompatible.
Type '<T>(q: IQuery<T>) => Observable<T>' is not assignable to type '<T>(q: IQuery<T>) => Observable<T>'.
Type 'Observable<any>' is not assignable to type 'Observable<any>'.
Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'. TypeScript Virtual Projects
如果返回类型为 T 但不是 则有效observable<T>
。
谢谢!