How can I extend Node's stream

时间:2016-07-11 21:40:12

标签: node.js typescript stream

I'm trying to create a method for Readable Stream, but after trying just a little bit, I ran out of ideas to how.

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

Can someone give me a solution and explain why the errors happened? Thanks

2 个答案:

答案 0 :(得分:1)

explain why the errors happened

The first rule of migrating from JavaScript to TypeScript:

Declare what you use.

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

Here Readable doesn't have the member you are looking for. If you want to add it, you need to declare it. Something like :

interface Readable {
  asdfasdfasdf: any;
}

答案 1 :(得分:0)

我设法扩展了它们。这种行为并不像我想象的那样不寻常(我仍然希望能解释“类型'可读'和”类型'类型'可读'的区别。代码:

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)
    }
}