【问题标题】:Using Bluebird throws a compilation error in typescript使用 Bluebird 在 typescript 中引发编译错误
【发布时间】:2018-12-06 01:04:04
【问题描述】:

我收到一个错误

Bluebird<{}>' is not assignable to type 'Bluebird<boolean>

编译以下代码时

import * as Promise from 'bluebird'

function getPromise() : Promise<boolean> {
    return new Promise((resolve, reject) => {
        resolve(true);
    })
}

let p : Promise<boolean> = getPromise();
p.then(a => console.log("Done"));

蓝鸟版本 - 3.5.1

节点版本 - 6.10.3

Typescript 版本 - 2.3.1

【问题讨论】:

    标签: node.js typescript promise bluebird


    【解决方案1】:

    这在较新版本的打字稿中不是问题,其中new Promise 的泛型参数将由预期的返回类型推断。但是,在 2.3 中,您必须将泛型参数显式指定为 Promise

    function getPromise() : Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            resolve(true);
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多