eduardogbg eduardogbg - 4 months ago 22
Node.js Question

How can I extend Node's 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

Answer

I managed to extend them. The behaviour wasn't as unusual as I thought (I still would appreciate an explanation on the difference of "type 'Readable'" and "type 'typeof Readable'". The code:

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