Search code examples
node.jstypescriptstream

Typescript 4.8: how to extends Node stream.Writable


I am trying to create a simple class implementing Node stream.Writable but it seems I can't get the syntax right, the compiler is always complaining:

enter image description here

enter image description here

enter image description here

I am not sure exactly what I am doing wrong. Any hint?

node: 16.17.0
@types/node: 16.11.64
typescript: 4.8.3


Solution

  • You need to declare the _write method outside of the constructor.

    class MyWritable extends Writable {
      constructor() {
        super({ objectMode: true });
      }
      
      // moved outside the constructor function.
      _write(chunk: any, encoding: BufferEncoding, callback: () => void) {
        //...
      }
    }
    

    Which works without errors. See Playground