我有一个 nodejs / typescript 2 项目并使用es6-promise https://github.com/stefanpenner/es6-promise包裹。
现在我想摆脱额外的包,因为我可以直接在打字稿中定位 ES6。
所以我删除了 es6-promise 包并将 tsconfig.json 更改为目标 es6。
{
"compilerOptions": {
"target": "es6",
// ...
}
}
许多第 3 方包使用 Bluebird Promise,但 Promise 定义与默认的 es6 Promise 不兼容,如 github 上不同帖子所述
- bluebird 3.0 定义不可分配给 ES6 Promises https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11027
- 提供一种在 es6 编译目标中全局加载 Bluebird 的方法。 https://github.com/Microsoft/TypeScript/issues/12382
-
将 Symbol.toStringTag 添加到 Promise 实例 https://github.com/petkaantonov/bluebird/issues/1277
所以我收到以下错误。
TS2322:类型“Bluebird”不可分配给类型“Promise”。 “Bluebird”类型中缺少属性“[Symbol.toStringTag]”。
npm 上还有其他类型的包@types/bluebird-global https://www.npmjs.com/package/@types/bluebird-global。
在一篇博客文章中,用户建议使用它而不是@类型/蓝鸟 https://www.npmjs.com/package/@types/bluebird但一些第 3 方软件包(例如,sequelizetypings)引用了 bluebird 而不是 bluebird-global,因此我收到另一个错误,因为缺少 bluebird 的类型。
让它发挥作用的好解决方案是什么?
我正在处理
TS2322:类型“Bluebird”不可分配给类型“Promise”。 “Bluebird”类型中缺少属性“[Symbol.toStringTag]”。
并找到了这个线程:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/10801 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/10801
TL;DR;它的版本是执行以下操作之一:
-
在每个 .ts 条目文件中添加以下内容以覆盖全局承诺:
import * as Bluebird from 'bluebird';
declare global { export interface Promise<T> extends Bluebird<T> {} }
Or
- 将所有 Promise 包装在 Bluebird Promise 构造函数中。这里有一些运行时开销,它在 Bluebird 网站上被列为反模式。
顺便说一句,我无法让第二个选项起作用,但第一个选项对我来说效果很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)