为什么fs.open返回一个数字? [英] Why does fs.open return a number?

查看:68
本文介绍了为什么fs.open返回一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码使用节点/打字稿读取文件:

I'm trying to use node/typescript to read a file using this code:

const openFile = util.promisify(fs.open);
write(data: Buffer, index: number) {
    if (!checkPieceIntegrity(data, this.pieceMap[index])) {
        return false;
    } else {
        return openFile(this.path, "r+")
            .then(fd => {
                const writeStream = createWriteStream(fd, {
                    flags: "r+",
                    start: index * this.pieceLength,
                    autoClose: true,
                })

                return util.promisify(writeStream.write)(data);
            })


    }
}

但是当我尝试使用 tsc 进行编译时,我得到了错误:

but when I try to compile with tsc I get and error:

类型为'number'的参数不能分配给类型为'PathLike'的参数.

关于此行:

const writeStream = createWriteStream(fd,{.

node.js文档说, fs.open 返回一个文件描述符,而不是数字,所以打字稿为什么认为 fd 是一个数字,我怎么得到它?将其识别为文件描述符?

The node.js docs say that fs.open returns a file descriptor, not a number so why does typescript think fd is a number and how can I get it to recognize it as a file descriptor?

推荐答案

fs.open 返回文件描述符(作为整数).但是 createWriteStream 会收到< string>|< Buffer>|< URL> 作为其第一个参数,而不是文件描述符.

fs.open returns a file descriptor (as an integer). But createWriteStream receives a <string> | <Buffer> | <URL> as its first argument, not a file-descriptor.

检查 fs.write ,该函数可以接收文件描述符并允许写入到文件.

Check fs.write for a function that receives a file descriptor and allows writing to the file.

这篇关于为什么fs.open返回一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆