SolvedDefinitelyTyped @types/readable-stream throws error at run-time

The following error was thrown from @types/readable-stream when starting a NestJS project.

Other observations:

  1. The @types/readable-stream was installed as a dependency for the mssql package.
    No error was thrown after the mssql package (and @types/readable-stream) was uninstalled
  2. No error was thrown from the previous version which uses string instead of BufferEncoding for encoding varables.

*** Error Logs Start **
[18:59:15] Starting compilation in watch mode...

node_modules/@types/readable-stream/index.d.ts:13:15 - error TS2417: Class static side 'typeof _Readable' incorrectly extends base class static side 'typeof Readable'.
The types of 'Stream.Readable.Writable' are incompatible between these types.
Type 'typeof _Readable.Writable' is not assignable to type 'typeof import("stream").Writable'.
Types of parameters 'options' and 'opts' are incompatible.
Type 'import("stream").WritableOptions' is not assignable to type '_Readable.WritableOptions'.
Type 'WritableOptions' is not assignable to type 'WritableStateOptions'.
Types of property 'defaultEncoding' are incompatible.
Type 'string' is not assignable to type 'BufferEncoding'.

13 declare class _Readable extends stream.Readable {
*** Error Logs End **

20 Answers

✔️Accepted Answer

I think there is double trouble here.

  1. In Node14 the stream transform method takes a BufferEncoding type instead of a vanilla string.
    Version 2.3.6 of this definition file assumes the user is on Node14. This should somehow be fixed to work on Node12 (current LTS) and Node14 (future LTS).

  2. The stream definition was recently updated in Node types. There are 2 new fields that impact Node12 and higher: #45175
    These fields have been carried over here in v2.3.7, but that version suffers from item 1 above.

For Node14, taking the latest definitions from both should work.

As a workaround for Node12, we can take the last version of both types before they started conflicting.
npm install --save-dev --save-exact @types/node@12.12.43
npm install --save-dev --save-exact @types/readable-stream@2.3.5

Hope we can get a fix for the first issue to make everybody happy!

Other Answers:

Yea it seems readable-stream isn't quite compatible with with pre v14 node versions anymore, this is partially my bad.

Run npm i -D -E @types/readable-stream@2.3.5 to downgrade the version, this will install a compatible version and make the version number static.

More Issues: