When trying to extend a class from a class in a node_modules
the typescript compiler throws a error saying:
Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.
This only happens when the base class is from a node_module
.
The base class looks like:
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'));
}
}
Subclassing it in a project:
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();
}
}
If the base class (TestBase
) is moved from the node_module
to the project it self and change the import to look like
import {TestBase} from "./base";
The error disappears.
Is this due to that the compiles creates the types in different scopes for each module? I'm completely lost here.
Update:
This seems to only happen when linking the node_modules
with npm link
.
Seems like one possible workaround for the moment is to instead of returning a type in the base class to return a interface.
More information can be found here: