I am trying to download a file with typescript (Node.js) using node-fetch.
As per the documentation and a stack overflow answer here the following code should work:
public async downloadXMLFeed(): Promise<void>{
    // function for download the file to
    // a temporary location
    let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
    fetch(FILE_URL)
    .then((response) => {
        response.body.pipe(fileStream)
        fileStream.on("finish", () => {
            fileStream.close();
        })
    });
but I get the following error:
Property 'pipe' does not exist on type 'Response'
I also checked the type file for response in node-fetch types. It does not have a pipe function.
I checked the type definition for node-fetch here and it seems the body in response is a readable stream as per the interface here:
export class Body {
   constructor(body?: any, opts?: { size?: number; timeout?: number });
   arrayBuffer(): Promise<ArrayBuffer>;
   blob(): Promise<Buffer>;
   body: NodeJS.ReadableStream; // This should work.
   bodyUsed: boolean;
   buffer(): Promise<Buffer>;
   json(): Promise<any>;
   size: number;
   text(): Promise<string>;
   textConverted(): Promise<string>;
   timeout: number;
}
and has a function pipeTo (I found this from the documentation here. I tried run the following code after going through the mentioned documentation:
public async downloadXMLFeed(): Promise<void>{
    // function for download the file to
    // a temporary location
    let fileStream = fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
    fetch(FILE_URL)
    .then((response) => {
        response.body.pipe(fileStream)
        fileStream.on("finish", () => {
            fileStream.close();
        })
    });
However I get the error:
Argument of type 'WriteStream' is not assignable to parameter of type 'WritableStream<Uint8Array>'.
Type 'WriteStream' is missing the following properties from type 'WritableStream': locked, abort, getWriter
Now the following code:
 fs.createWriteStream(FILE_PATH, {encoding: "utf-8"});
returns an object with type WriteStream (checked here) which implements stream.Writable. So I don't understands why doesn't the WriteStream object have the mentioned functions.
Also, how do I solve this? Is there a standard way of downloading http files in typescript with a node.js backend that I am not being able to figure out? Or am I missing something here.
 
    