Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeScript在ES5中与Bluebird一起使用动态导入_Javascript_Typescript_Ecmascript 6_Promise_Bluebird - Fatal编程技术网

Javascript TypeScript在ES5中与Bluebird一起使用动态导入

Javascript TypeScript在ES5中与Bluebird一起使用动态导入,javascript,typescript,ecmascript-6,promise,bluebird,Javascript,Typescript,Ecmascript 6,Promise,Bluebird,我试图在TypeScript中使用新的动态import()函数,但出现以下错误: TS2712:ES5/ES3中的动态导入调用需要“承诺” 构造器。确保你有“承诺”的声明 构造函数或在--lib选项中包含“ES2015” 我可以像消息所建议的那样在我的tsconfig中包含ES2015.promiselib,但这会使我在使用Bluebird promises时失去类型安全性 我知道可以在TypeScript中使用Bluebird来表示async/await,所以我想这也应该以同样的方式工作 该

我试图在TypeScript中使用新的动态
import()
函数,但出现以下错误:

TS2712:ES5/ES3中的动态导入调用需要“承诺” 构造器。确保你有“承诺”的声明 构造函数或在
--lib
选项中包含“ES2015”

我可以像消息所建议的那样在我的tsconfig中包含
ES2015.promise
lib,但这会使我在使用Bluebird promises时失去类型安全性

我知道可以在TypeScript中使用Bluebird来表示
async/await
,所以我想这也应该以同样的方式工作


该消息还提到这一点:

确保您有“承诺”构造函数的声明或[…]

是否可以声明Bluebird构造函数用作TS中的承诺构造函数


示例代码:


TypeScript正在寻找全局
承诺
。代码中包含的是在模块(“蓝鸟”)中声明并在另一个模块中本地使用的
承诺

以下是解决编译错误并获得可运行代码的最简单方法:

test.ts

import * as Bluebird from 'bluebird';

declare global {
    const Promise: {
        new <R>(callback: (
            resolve: (thenableOrResult?: R | PromiseLike<R>) => void,
            reject: (error?: any) => void,
            onCancel?: (callback: () => void) => void
        ) => void): Bluebird<R>;
    };
}

import('jquery').then($ => {
    console.log($);
});

这里有几个不必要的选项,处理问题需要使用
skipLibCheck
@types/jquery
TypeScript正在寻找一个全局
承诺。代码中包含的是在模块(“蓝鸟”)中声明并在另一个模块中本地使用的
承诺

以下是解决编译错误并获得可运行代码的最简单方法:

test.ts

import * as Bluebird from 'bluebird';

declare global {
    const Promise: {
        new <R>(callback: (
            resolve: (thenableOrResult?: R | PromiseLike<R>) => void,
            reject: (error?: any) => void,
            onCancel?: (callback: () => void) => void
        ) => void): Bluebird<R>;
    };
}

import('jquery').then($ => {
    console.log($);
});

这里有几个不必要的选项,
skipLibCheck
是处理问题所必需的
@types/jquery

jquery
模块只是一个例子,我实际上是在浏览器中运行它,所以这不会有问题。但非常感谢您提供的有效解决方案!
jquery
模块只是一个示例,实际上我正在浏览器中运行它,所以这不会有问题。但非常感谢您提供的有效解决方案!
import * as Bluebird from 'bluebird';

declare global {
    const Promise: {
        new <R>(callback: (
            resolve: (thenableOrResult?: R | PromiseLike<R>) => void,
            reject: (error?: any) => void,
            onCancel?: (callback: () => void) => void
        ) => void): Bluebird<R>;
    };
}

import('jquery').then($ => {
    console.log($);
});
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "removeComments": true,
    "sourceMap": true,
    "alwaysStrict": true,
    "forceConsistentCasingInFileNames": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
    "allowJs": true,
    "skipLibCheck": true,
    "lib": ["es5", "dom", "es2015.collection"]
  }
}