2016-07-03 181 views
29

派生的類當試圖從一個類中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

+2

我認爲你是對的。您不能擴展具有全局/環境範圍的類。 [Typescript GitHub repo上的此線程](https://github.com/Microsoft/TypeScript/issues/3282)似乎描述了一些解決方法。 – morphatic

+1

即使沒有'npm link',我也有這個問題。我嘗試使用'npm pack'和'npm install'作爲解決方法,但沒有骰子。 https://github.com/robertjd/sp-ng2/pull/1 –

+0

@Maxime Raineville的答案爲我工作 – vincecampanale

回答

3

我有同樣的問題。

我有以下目錄結構(angular2項目)

angular 
| 
---- common_files 
     | 
     ----- package.json 
     | 
     ----- index.ts 
     | 
     ----- catalog1 
      | 
      ---- package.json 
      | 
      ---- some_file_with_service_model_comopnent.ts 
      | 
      ---- index.ts - this is regular barrel file 
     | 
     ----- catalog2 
| 
---- app1 
    | 
    ------ package.json 
| 
---- apps 
    | 
    ------ package.json 

在我的共同我已經definied對象和服務:

export class ItemBase {} 

esport class SomeType extends ItemBase {} 

export class ItemServiceBase { 
    public getItems():Observable<ItemBase> { 
     //do something 
    } 
} 

export class SomeService extends ItemServiceBase { 
    //some specific operations can go here 
} 

在我的應用我是用從普通項目如下:

import { SomeType, SomeTypeService } from "warehouse-system/products-settings"; 

class AttributeTypesComponent { 
    private myValues : Observable<SomeType[]>; 
    private service : SomeTypeService; 

    constructor(){ 
     this.service = new SomeTypeService(); 
     this.myValues = <Observable<SomeType[]>> this.service.getItems(); 
    } 
} 

這導致了編譯問題: ERROR in [at-loader] src/app/some_file.ts:22:47 Type 'Observable<ItemBase[]>' cannot be converted to type 'Observable<SomeType[]>'. Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.

調查後我改變了myValues的類型,但它仍然沒有解決問題。編譯問題改爲:

ERROR in [at-loader] src/app/some_file.ts:22:47 Type 'Observable<SomeType[]>' cannot be converted to type 'Observable<SomeType[]>'. Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.

最終的解決方案(解決方法)

什麼解決我的問題,是 「改寫」 觀察到的應用程序方面:

this.myValues = Observable.create(subscriber => { 
     this.service.getItems().subscribe(items => subscriber.next(items)); 
    }); 

這是不是很好的解決方法。在我看來,這個問題是由npm/typescript/observables中的一個錯誤引起的。但直到問題不解決打字機開發方面,您可以使用此解決方法作爲解決方案。

11

我在爲項目開發自定義模塊時遇到了一個非常類似的問題。我不是100%確定我們遇到了同樣的問題,但它看起來非常接近。

我如何解決我的問題

我建議你刪除文件夾node_modules並重新安裝所有的依賴關係。

rm -rf node_modules/ 
npm cache clean 
npm install 

爲我解決了這個問題。

什麼問題是(我認爲)

我開發的模塊有這樣的主體工程正試圖擴展一個抽象類。然而,當試圖編譯主項目時,編譯器會拋出同樣的錯誤。

經過一番探索後,我注意到在將我的模塊安裝到我的項目中時,NPM會抱怨UNMET PEER DEPENDENCY。當在項目的node_modules裏面看時,我注意到我的模塊有另一個嵌套在其中的node_modules文件夾,它具有與主項目共享的一些依賴關係。

我對此並不確定,但我認爲NPM認爲模塊期望與主項目共享不同版本的依賴項。

因此,模塊內部的抽象類從其自己嵌套的node_modules文件夾引用Observable,而主項目引用頂級node_modules文件夾中的Observable。

更多信息

試圖解決我的問題時,提供一些見解對於我這個其他問題:

Why does npm install say I have unmet dependencies?

+0

對於任何尋找解決方案的人來說,這一個爲我工作。 – vincecampanale

+0

有類似的問題,刪除npm包中的'node_modules'解決了這個問題。好的呼叫伴侶。 –

3

我得到一個瘋狂的錯誤,如

Type 'Observable<MessageEvent>' cannot be converted to type 'Observable<MessageEvent>' 
Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>' 

其原因是npm link(實際上是npm i ../other-project,但它是完全相同的東西)。

我的解決方法是有點欺騙:

(stream as any as Observable<MessageEvent>) 

LOL

3

如果有,你必須添加在主機應用程序的tsconfig路徑映射多個node_modules文件夾。只需將@ rxjs/*映射到根節點node_modules/rxjs/*即可。然後一切正常。