eduardogbg
eduardogbg

Reputation: 573

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

Upvotes: 0

Views: 1512

Answers (2)

eduardogbg
eduardogbg

Reputation: 573

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

Upvotes: 0

basarat
basarat

Reputation: 276085

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

Upvotes: 1

Related Questions