简体   繁体   English

TypeScript 中 createReadStream 的类型应该是什么?

[英]What should be the type of createReadStream in TypeScript?

I am trying to use typescript.我正在尝试使用打字稿。 What should be the type of createReadStream ? createReadStream类型应该是什么? I have found the types declaration following from fs.d.ts file from this DefinitelyTyped link on github我从github上的这个绝对类型链接的fs.d.ts文件中找到了类型声明

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在官方node/fs.d.ts文件中找到的类型

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*/
};

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. fs.createReadStream返回一个ReadStream对象。 Also, the TypeScript doesn't expect you to provide a type for fs.createReadStream .此外,TypeScript 不希望您为fs.createReadStream提供类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM