【问题标题】:Typescript cannot infer type when I use Bluebird.promisifyAll当我使用 Bluebird.promisifyAll 时,打字稿无法推断类型
【发布时间】:2018-11-16 02:18:36
【问题描述】:

我想用 Bluebird 来 promiseAll nodejs fs modules 和 Typescript

我的代码

import * as FS from 'fs'

const fs = Bluebird.promisifyAll(FS)

interface fs{
    readFileAsync: Function
}

但是当我使用fs.readFileAsync()

tsc 告诉我

[ts] Property 'readFileAsync' does not exist on type 'typeof import("fs")'. Did you mean 'readFileSync'? [2551]

如何告诉 tsc '嗨,fs 对象中还有一个名为 readFileAsync 的函数'?

【问题讨论】:

  • 您确定fs.readFileAsync 存在吗?如果您在谈论节点文件系统模块,它不包含readFileAsync 函数。
  • 顺便提一下,Node v10 及更高版本有fs.promises,这是一个将每个函数实现为异步的命名空间。

标签: typescript bluebird


【解决方案1】:

创建一个名为fs 的接口不会自动使fs 常量实现它。

如果您想手动编写该函数,您可以按照以下方式进行操作:

import * as FS from 'fs'

interface fs {
    readFileAsync: Function
}

const fsWithReadFileAsync: fs = {
    ...Bluebird.promisifyAll(FS),
    readFileAsync: () => {
        // Implement your function here
    }
}

fsWithReadFileAsync.readFileAsync()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 2020-04-02
    • 2020-12-20
    • 2016-08-03
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多