js-promise-socket icon indicating copy to clipboard operation
js-promise-socket copied to clipboard

No longer compiles with tsc

Open jcwatson11 opened this issue 2 years ago • 2 comments

When anything that uses promise-socket is compiled with the TypeScript compiler (tsc), the following error occurs:

node_modules/promise-socket/node_modules/promise-duplex/lib/promise-duplex.d.ts:6:11 - error TS2430: Interface 'DuplexStream' incorrectly extends interface 'Duplex'.
  Property 'closed' is optional in type 'DuplexStream' but required in type 'Duplex'.

6 interface DuplexStream extends Duplex {
            ~~~~~~~~~~~~


Found 1 error.

This is because the dependency package promise-duplex (published by the same github user) incorrectly extends the Duplex class in the promise-duplex.ts file.

A child class cannot change the required-ness of a property that exists in a parent class to make it optional when it is required in the parent class. TypeScript requires that child classes must implement the entire contract of a parent class. If a field is required in the parent, it must be required in the child.

jcwatson11 avatar Mar 21 '23 23:03 jcwatson11

I've created a similar issue in the promise-duplex project: https://github.com/dex4er/js-promise-duplex/issues/61

But I'm posting it here so users know this package is a dead-end until this is fixed.

jcwatson11 avatar Mar 21 '23 23:03 jcwatson11

Yep, broken for me too. I came up with this snippet which covers my needs for now.

class PromiseSocket {
  constructor(private innerSok: net.Socket) {}

  public connect(port: number, host: string): Promise<void> {
    return new Promise((resolve, reject) => {
      this.innerSok.connect(port, host, () => {
        resolve();
      });
      this.innerSok.on('error', err => {
        reject(err);
      });
    });
  }

  async write(data: string) {
    return new Promise<void>((resolve, reject) => {
      this.innerSok.write(data, () => {
        resolve();
      });
      this.innerSok.on('error', err => {
        reject(err);
      });
    });
  }

  async readAll() {
    return new Promise<string>((resolve, reject) => {
      let buf = '';
      this.innerSok.on('data', data => {
        buf += data;
      });
      this.innerSok.on('error', err => {
        reject(err);
      });
      this.innerSok.on('end', () => {
        resolve(buf);
      });
    });
  }
}

snarky-puppy avatar Aug 22 '23 04:08 snarky-puppy

Fixed in https://github.com/dex4er/js-promise-socket/releases/tag/v8.0.0

dex4er avatar Jun 21 '24 13:06 dex4er