IE11中的打字稿和承诺

时间:2017-08-27 05:36:44

标签: typescript promise internet-explorer-11 fetch

我在使用Webpack进行编译时使用的是Typescript(v2.4.2)。一切都编译得很好,但是当我在IE11中运行我的代码时,我收到以下错误:' Promise'未定义。

这是我的tsconfig:

{
    "compilerOptions": {
        "outDir": "./wwwroot/js",
        "sourceMap": true,
        "allowJs": true,
        "lib": [
            "dom",
            "es5",
            "scripthost",
            "es2015.iterable"
        ],
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "skipDefaultLibCheck": true,
        "types": [ "es6-promise" ]
    },
    "include": [
        "./Ts/**/*"
    ]
}

这是我的webpack配置:

const path = require('path');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
    entry: {
        main: "./Ts/main.ts"
    },
    output: {
        filename: "[name].js",
        path: path.resolve(__dirname, "wwwroot/js")
    },
    devtool: "source-map",
    resolve: {
        extensions: [".ts", ".js"]
    },
    module: {
        rules: [
            { test: /\.ts?$/, loader: "awesome-typescript-loader" },
            { enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: Infinity
        })
    ]
};

我知道我需要某种polyfill,但我不确定是什么或如何实现它。到目前为止,我所见过的所有东西都建议使用像Promise和Whatwg-Fetch这样的polyfill,但每当我添加它们时,我都会遇到编译错误。

1 个答案:

答案 0 :(得分:4)

IE 11本身不支持Promise,这与Typescript无关(Typescript libs实际上没有为代码添加任何东西)

您可以使用bluebird作为Promise库

npm install --save bluebird

还安装类型: npm install --save-dev @types/bluebird

import * as Promise from "bluebird"