3

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.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
shuri
  • 91
  • 3
  • 12
  • 1
    Here tsc probably doesn't know about the properties of node-fetch response object. Try installing types for node-fetch: "npm i -D @types/node-fetch". – h-sifat Jul 30 '21 at 11:55
  • Ah indeed that was exactly it and it also required an import, while using fetch typically doesn't. – shuri Jul 30 '21 at 12:22

1 Answers1

3

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.

shuri
  • 91
  • 3
  • 12