Preston
Preston

Reputation: 167

Typescript and promises in IE11

I'm working in Typescript (v2.4.2) using Webpack to compile. Everything compiles fine, but when I run my code in IE11, I get the following error: 'Promise' is undefined.

Here's my 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/**/*"
    ]
}

Here's my webpack config:

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
        })
    ]
};

I understand that I need some sort of polyfill, but I'm not sure what or how to implement it. Everything I've seen so far suggests certain polyfills like Promise and Whatwg-Fetch, but whenever I add them I get compile errors.

Upvotes: 2

Views: 4920

Answers (1)

Daniel Krom
Daniel Krom

Reputation: 10068

Promises are not supported natively in IE 11 and this has nothing to do with Typescript (Typescript libs doesn't actually adds anything to the code)

You can use bluebird as Promise library

npm install --save bluebird

Also install types: npm install --save-dev @types/bluebird

import * as Promise from "bluebird"

Upvotes: 5

Related Questions