What should be the type of createReadStream in TypeScript?

Viewed 5359

I am trying to use typescript. What should be the type of createReadStream? I have found the types declaration following from fs.d.ts file from this DefinitelyTyped link on github

UPDATE

what I am trying to do is upload a file from the frontend. On backend( basically node), I receive a file object. on destructing the file object, I get the following.

const { createReadStream, filename, mimetype, encoding } = await file;

Is there any way, I can use this type?

updated question

Now, how should I add the type of the file object that I receive in the function parameter of the function given below?

export const processUpload = async (file, DestinationDir) {

}

Type found in official node/fs.d.ts file

function createReadStream(path: PathLike, options?: string | {
        flags?: string;
        encoding?: string;
        fd?: number;
        mode?: number;
        autoClose?: boolean;
        /**
         * @default false
         */
        emitClose?: boolean;
        start?: number;
        end?: number;
        highWaterMark?: number;
    }): ReadStream;

Code sample

export const processUpload = async (
  file:
    | PromiseLike<{
        createReadStream: /** WHAT SHOULD BE THE TYPE */;
        filename: string;
        mimetype: string;
        encoding: string;
      }>
    | {
        createReadStream: /** WHAT SHOULD BE THE TYPE */;
        filename: string;
        mimetype: string;
        encoding: string;
      },

  { DestinationDir = "default" }: { DestinationDir: string }
) => {
  const { createReadStream, filename, mimetype, encoding } = await file;
  const stream = createReadStream();
  
  /** SOME CODE */ 

  return /*SOME RETURN TYPE*/
};
2 Answers

I think you could use this:

import { ReadStream } from "fs-capacitor";

export interface FileUpload {
  createReadStream(): ReadStream;
  filename: string;
  mimetype: string;
  encoding: string;
}

fs.createReadStream returns a ReadStream object. Also, the TypeScript doesn't expect you to provide a type for fs.createReadStream.

Related