【发布时间】:2017-12-22 06:39:32
【问题描述】:
我正在尝试在 TypeScript 中使用新的动态 import() 函数,但出现以下错误:
TS2712: ES5/ES3 中的动态导入调用需要“Promise” 构造函数。确保您有“承诺”的声明 构造函数或在
--lib选项中包含“ES2015”。
我可以像消息提示的那样在我的 tsconfig 中包含 ES2015.promise 库,但这会使我失去类型安全性,因为我正在使用 Bluebird 承诺。
我知道可以在 TypeScript 中将 Bluebird 用于 async/await,所以我想这也应该以同样的方式工作。
消息中还提到了这一点:
确保您有“Promise”构造函数的声明或 [...]
是否可以将Bluebird构造函数声明为TS中的Promise构造函数?
示例代码:
import * as Bluebird from 'bluebird';
// This works
async function exampleAsync(): Bluebird<number> {
const result = await Bluebird.resolve(5);
return result;
}
// This does not
import('jquery').then($ => {
console.log($.fn.jquery);
});
TSConfig:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"removeComments": true,
"sourceMap": true,
"alwaysStrict": true,
"forceConsistentCasingInFileNames": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true,
"allowJs": true,
"typeRoots": ["node_modules/@types"],
"lib": ["es5", "dom", "es2015.collection"]
},
"exclude": ["node_modules"]
}
【问题讨论】:
标签: javascript typescript ecmascript-6 promise bluebird