繁体   English   中英

如何声明“Promise”构造函数?

[英]How to declare 'Promise' constructor?

我有一个针对 ES5 的旧 TypeScript 项目,但由于多种因素,无法升级到任何更新的项目,例如 ES2015。 在尝试利用 async/await 编译代码时,出现错误:

TS2705 (TS) An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your TS2705 (TS) An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your --lib option. TS2705 (TS) An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your option.

该错误表明有 2 个选项可以解决它,升级到 ES2015 的选项对我来说不是一个选项,因此我想知道如何准确地声明 'Promise' 构造函数。

我搜索了论坛并发现了许多类似的问题,但我能找到的所有建议最终都建议升级到至少 ES2015,正如我上面指出的,这对我来说不是一个选择。

尝试在js中找到Promise类的源码,注入global.Promise = 'what you copied'

或者您可以使用其他一些库,如 bluebird - 以同样的方式。

global.Promies = require('bluebird')()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM