Search code examples
node.jstypescriptfetch-apinode-fetch

Streaming response body to file in typescript: Property 'pipe' does not exist on type 'ReadableStream<Uint8Array>'


I am able to fetch a binary body from an API to write it to a file in node.

const fileStream = fs.createWriteStream(filePath);


fetch(apiURL).then((downloadResponse) => {
  downloadResponse.body.pipe(fileStream);

});

However, when doing so I get a linting error of:

Property 'pipe' does not exist on type 'ReadableStream'

It seems weird to me that the call would work when the linter gives an error. I even initially thought my logic was wrong and wasted time debugging this working call...

Is my typescript version misidentifying the type for some reason or should I not be able to perform this call ?

I am barely beginning with typescript but on occasion I run into such idiosyncrasies that slow me down when I am doing something that would seem perfectly valid.


Solution

  • The answer provided in the comments was indeed correct but the context in which this wasn't straight-forward was in a nextjs app using fetch from a server side rendering function such as getServerSideProps.

    On the client side it is pretty straight-forward that the standard fetch api is used however on SSR it wasn't evident that one had to additionnally install the types for node-fetch using npm i @types/node-fetch.