gpt4 book ai didi

javascript - ES6 Promise/Typescript 和 Bluebird Promise

转载 作者:可可西里 更新时间:2023-11-01 02:26:44 25 4
gpt4 key购买 nike

我有一个 nodejs/typescript 2 项目并使用 es6-promise包裹。现在我想去掉额外的包,因为我可以直接在 typescript 中定位 ES6。

所以我删除了 es6-promise 包并将 tsconfig.json 更改为目标 es6。

{
"compilerOptions": {
"target": "es6",
// ...
}
}

许多第 3 方包使用 Bluebird promise ,但 promise 定义与 github 上不同帖子中所述的默认 es6 promise 不兼容

所以我收到以下错误。

TS2322: Type 'Bluebird' is not assignable to type 'Promise'. Property '[Symbol.toStringTag]' is missing in type 'Bluebird'.

npm 上还有一个其他类型的包 @types/bluebird-global .在一篇博文中,用户建议使用它而不是 @types/bluebird但是一些第 3 方包(例如 sequelize typings)引用了 bluebird 而不是 bluebird-global 所以我得到另一个关于缺少 bluebird 类型的错误。

什么是让它发挥作用的好解决方案?

最佳答案

我正在处理

TS2322: Type 'Bluebird' is not assignable to type 'Promise'. Property '[Symbol.toStringTag]' is missing in type 'Bluebird'.

并找到了这个线程: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/10801

TL;DR;它的版本是执行以下操作之一:

  1. 在您的每个 .ts 入口文件中添加以下内容以覆盖全局 promise :

    import * as Bluebird from 'bluebird';

    declare global { export interface Promise<T> extends Bluebird<T> {} }

或者

  1. 将所有 promise 包装在 Bluebird promise 构造函数中。这里有一些运行时开销,它在 Bluebird 的站点上被列为反模式。

顺便说一句,我无法使用第二个选项,但第一个对我来说效果很好。

关于javascript - ES6 Promise/Typescript 和 Bluebird Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42689713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com