title: Using Array.reduce on Node Streams with Typescript
body: <p>I am fairly new to typescript and am not familiar with the more advanced type declarations. </p>
  
  <p>I am attempting to use reduce to chain together a series of node.js stream transforms onto an incoming node.js stream.</p>
  
  <p>The incoming stream is an object that extends <code>stream.Readable</code>. The stream transform objects all extend <code>stream.Transform</code>. </p>
  
  <p>I have an array of these transform objects and I essentially want the final result to be:</p>
  
  <p><code>let outStream = inStream.pipe(transform1).pipe(transform2).pipe(transform3)</code></p>
  
  <p>This works fine when manually piping each element, however I am unable to determine the correct typescript declaration to achieve this using <code>Array.prototype.reduce</code>. </p>
  
  <p>My code looks as follows:</p>
  
  <pre><code>class MyReadable extends stream.Readable { ... }
  class MyTransform extends stream.Transform { ... }
  
  let inStream = new MyReadable( /* ... */ )
  
  let transforms = [
    new MyTransform( /* ... */ ),
    new MyTransform( /* ... */ ),
    new MyTransform( /* ... */ )
  ];
  
  let outStream = transforms.reduce(
    (stream: MyReadable, transform: MyTransform): MyReadable =&gt; (
      stream.pipe(transform);
    ),
    inStream
  );
  </code></pre>
  
  <p>This throws a typescript error because <code>.pipe(transform)</code> returns a <code>MyTransform</code> type which is not compatible with <code>MyReadable</code> type which extends <code>stream.Readable</code>.</p>
  
  <p>I have tried doing:</p>
  
  <pre><code>let stream = transforms.reduce(
    (stream: ReadableStream, transform: MyTransform): ReadableStream =&gt; (
      stream.pipe(transform);
    ),
    inStream
  );
  </code></pre>
  
  <p>because <code>Readable</code> implements <code>NodeJS.ReadableStream</code> and <code>stream.Transform</code> implements <code>ReadWriteStream</code> which extends <code>NodeJS.ReadableStream</code> but type script says that <code>ReadableStream</code> does not have a <code>pipe</code> method (even though it is declared in the typescript declaration for <code>ReadableStream</code>.</p>
  
  <p>Given that both <code>Transform</code> and <code>Readable</code> implement pipe is there a way in typescript to coerce these together?</p>