簡體   English   中英

TypeScript 中 createReadStream 的類型應該是什么?

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

我正在嘗試使用打字稿。 createReadStream類型應該是什么? 我從github上的這個絕對類型鏈接的fs.d.ts文件中找到了類型聲明

更新

我想要做的是從前端上傳文件 后端(基本上是節點),我收到一個文件對象 銷毀文件對象時,我得到以下信息。

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

有什么辦法,我可以使用這種類型嗎?

更新的問題

現在,我應該如何在下面給出的函數的函數參數中添加我收到的文件對象的類型?

export const processUpload = async (file, DestinationDir) {

}

在官方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;

代碼示例

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

我想你可以用這個:

import { ReadStream } from "fs-capacitor";

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

fs.createReadStream返回一個ReadStream對象。 此外,TypeScript 不希望您為fs.createReadStream提供類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM