0

I implemented the Readable stream in Typescript. Here's how :

import * as stream from 'stream';

export class Readable extends stream.Readable {

    lastLineData = '';
    objectMode = true;
    ended = false

    constructor() {
        super({objectMode:true});
    }

    _read = () => {}

    send = data => {
        if (!this.ended) {
            this.push(data)
        }
    }

    errorAndCancel = err => {
        if (!this.ended) {
            this.emit('error', err)
            this.push(null)
            this.ended = true
        }
    }

    end = () => {
        if (!this.ended) {
            this.push(null)
            this.ended = true
        }
    }
}

On some machine (Gitlab CI with electronuserland/wine-02.18) or Windows Server machine with node.js v10 give me this error :

[08:11:38]  typescript: C:/git/celliers/synchro-pos/src/providers/xmlStreamParser/Readable.ts,

        Class 'Readable' defines instance member function '_read', but extended class 'Readable' defines it as
        instance member property.

  L13:      _read = () => {}

If I change _read to read, it doesn't work neither (same message). I question my implementation is not right. Is anyone able to tell me what I'm doing wrong?

Thanks!

1 Answer 1

1

In Typescript v.3 this would not be a problem. However you must be using an older version, where what you are trying to do is not allowed.

The error says that your class is defining _read as a property, whereas the parent class defines it as a function. Therefore your child class is incorrectly extending its parent. You need to either upgrade to Typescript 3, or to change your property:

_read = () => {}

To a method:

_read() {}
Sign up to request clarification or add additional context in comments.

1 Comment

I used your suggestion instead of upgrading to Typescript 3. Thanks it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.