Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

es6 Map() doesn't compile to es5 when using Typescript

I've just started using Typescript in a project for the first time.

I really want to use Map() to organize a small array of key value pairs.

Unfortunately when the typescript is compiled (using gulp and gulp-typescript) to vanilla, ES5 JavaScript, Map() still exists. I need this code to work in ie9.

Any advice at this stage would be greatly appreciated.

like image 755
fu8ar Avatar asked Nov 04 '16 16:11

fu8ar


2 Answers

Map is a part of ES6 and have nothing to do with TypeScript.

If you want tot use Map in ES5 environment you should include an appropriate polyfill.

My first two thoughts are:

  • es6-map module
  • core-js/es6/map from core-js generic ES6 polyfill library
like image 118
Leonid Beschastny Avatar answered Oct 10 '22 18:10

Leonid Beschastny


You'll need a shim or a polyfill.

I've used es6-map once and it's pretty good.

Unfortunately there is no TypeScript definitions for it, so my suggestion would be using es6-shim directly from a CDN and add this to your tsconfig.json.

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6"]
    }
}

You'll be able to use ES6 Map strongly typed, your code will be compiled down to ES5 and old browsers would support it through the shim.

like image 24
goenning Avatar answered Oct 10 '22 20:10

goenning