使用 Bluebird 会在打字稿中引发编译错误

Using Bluebird throws a compilation error in typescript

我收到一个错误

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

打字稿版本 - 2.3.1

这在较新版本的打字稿中不是问题,其中 new Promise 的通用参数将由预期的 return 类型推断出来。但是在 2.3 中,您必须显式指定通用参数 Promise:

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