ES6承诺/打字稿和蓝鸟承诺 [英] ES6 Promise / Typescript and the Bluebird Promise

查看:109
本文介绍了ES6承诺/打字稿和蓝鸟承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个nodejs / typescript 2项目,并使用 es6-promise 软件包。
现在我想摆脱额外的包,因为我可以直接在打字稿中定位ES6。

I have a nodejs / typescript 2 project and use the es6-promise package. Now i would like to get rid of the extra package because i can target ES6 in typescript directly.

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

So i removed the es6-promise package and changed the tsconfig.json to target es6.

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

许多第三方软件包使用Bluebird承诺,但承诺定义与默认的es6承诺不相容,如不同github上的帖子

Many 3rd party packages uses the Bluebird promise but the promise definition is incompatible to the default es6 promise as stated on different posts on github

  • bluebird 3.0 definifion is not assignable to ES6 Promises
  • Provide a way to load Bluebird globally in es6 compilation target.
  • Add Symbol.toStringTag to promise instance

所以我收到以下错误。


TS2322:类型'Bluebird'不能分配给'Promise'类型。 Bluebird类型中缺少[Symbol.toStringTag]属性。

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

npm @ types / bluebird-global
在其中一篇博文中,用户建议使用它而不是 @ types / bluebird 但是一些第三方软件包(例如sequelize typings)引用了bluebird而不是bluebird-global所以我得到了蓝鸟缺失的另一个错误。

There is a other types package on npm @types/bluebird-global. In one of the blog post a user suggests to use it instead of @types/bluebird but some 3rd party packages (e.g. sequelize typings) reference bluebird and not bluebird-global so i get another error for the missing typings of bluebird.

什么是一个很好的解决方案让这个工作?

What is a good solution to get this to work?

推荐答案

我正在处理


TS2322:类型'Bluebird'不能分配给'Promise'类型。属性'[Symbol.toStringTag]'在'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;它的版本是执行以下操作之一:

The TL;DR; version of it is to do one of the following:


  1. 在每个.ts条目文件中添加以下内容以覆盖全球承诺:

  1. In each of your .ts entry files add the following to overwrite the global promise:

从'bluebird'导入*作为Bluebird;

声明全局{export interface Promise< T>扩展Bluebird< T> {}}


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

顺便说一句,我无法获得第二种选择,但第一种选择对我来说没问题。

As an aside, I could not get the second option to work, but the first worked fine for me.

这篇关于ES6承诺/打字稿和蓝鸟承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆