派生的类当试图从一个类中node_modules
打字稿编译器抛出一个错误扩展类的说法:“可观察<T>”不是从“可观察<T>”
Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.
这只是发生在基类来自node_module
。
的基类的样子:
import {Observable} from "rxjs/Observable";
export abstract class TestBase<T> {
request(options: any):Observable<T> {
return Observable.throw(new Error('TestBase is abstract class. Extend it and implement own request method'));
}
}
项目中的子类是:
import {Observable} from "rxjs/Observable";
import {TestBase} from "@org/core";
class SocketResponse {
}
class Socket {
request(): Observable<SocketResponse> {
return new Observable.of(new SocketResponse());
}
}
export class Sub extends TestBase<SocketResponse> {
request(options:any):Observable<SocketResponse> {
return new Socket().request();
}
}
如果基类(TestBase
)从node_module
移动到项目它的自我与改变导入看起来像 import {TestBase} from "./base";
错误消失。
这是由于编译创建每个模块的不同范围的类型?我完全迷失在这里。
更新:
这似乎与npm link
链接node_modules
时才会发生。 似乎有一种可能的解决方法,就是不用返回基类中的类型来返回接口。
更多信息可以在这里找到:
https://github.com/Microsoft/TypeScript/issues/6496
https://github.com/ReactiveX/rxjs/issues/1744
我认为你是对的。您不能扩展具有全局/环境范围的类。 [Typescript GitHub repo上的此线程](https://github.com/Microsoft/TypeScript/issues/3282)似乎描述了一些解决方法。 – morphatic
即使没有'npm link',我也有这个问题。我尝试使用'npm pack'和'npm install'作为解决方法,但没有骰子。 https://github.com/robertjd/sp-ng2/pull/1 –
@Maxime Raineville的答案为我工作 – vincecampanale