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

问题描述 投票:1回答:1

我正在尝试使用打字稿。 createReadStream的类型应该是什么?我从github

fs.d.tsDefinitelyTyped link文件中找到了类型声明。

有什么办法,我可以使用这种类型吗?

在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*/
};
node.js typescript fs
1个回答
0
投票

安装@types/node

这里是从头开始的步骤:

  1. 为项目创建目录。

  2. 初始化它:

    npm init
    
  3. 安装Node.js的TypeScript和类型:

    npm install typescript @types/node
    
  4. ((可选)通过将"type": "module"添加到package.json来表示您正在使用ESM。

  5. 创建TypeScript文件,例如index.ts

  6. 在该文件中,您现在可以:

    import {createReadStream} from "fs";
    
    createReadStream("filename");
    

将正确应用createReadStream的类型。

该文件的内容可能是:

import * as fs  from "fs";

fs.createReadStream("filename");

...如果您愿意。

© www.soinside.com 2019 - 2024. All rights reserved.