2016-07-11 40 views
0

我試圖創建一個可讀流的方法,但嘗試一點點後,我跑出了想法如何。如何擴展節點的流

import * as stream from 'stream' 
//yields Property 'asdasas' does not exists on type 'Readable' 
stream.Readable.prototype.asdasas 
//yields asdas does not exists on type 'typeof Readable' 
stream.Readable.asdas 

有人可以給我一個解決方案,並解釋爲什麼發生錯誤?由於

回答

0

我設法擴展它們。該行爲是不是不尋常,因爲我想(我還是會喜歡上的差異的解釋「型‘讀’」和「型‘的typeof讀’」的代碼:

import * as stream from 'stream' 

class mod_Readable extends stream.Readable { 
    pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean; }): T { 
     //whatever 
     return super.pipe(destination, options) 
    } 
} 
1

解釋爲什麼錯誤發生

從JavaScript遷移到打字稿的第一條規則:

聲明你用什麼

https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

這裏Readable沒有你要找的成員。如果你想添加它,你需要聲明它。例如:

interface Readable { 
    asdfasdfasdf: any; 
} 
+0

我使用節點的類型定義文件,在那裏我可以找到Readable類。爲什麼我應該聲明接口,即使已經有一個帶有這個名字的類? – eduardogbg