3

I want to use ES6 code (such as collections) in an Angular 4+ project built with angular-cli. Here is the tslint.json I'm using:

{
  "compilerOptions": {
    "baseUrl": "",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["dom","es2017"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es6",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

Note that target has to be es6, since Typescript can't transpile ES6 code to ES5.

Unfortunately, angular-cli uses webpack2 which seems to have a hardwired dependency on UglifyJS which is incompatible with ES6 and it is unlikely this will be fixed anytime soon. I could choose not to use ES6 code or angular-cli, but before I give up on either, I'm wondering if there's a reasonable workaround for this issue?

4
  • 1
    Angular CLI uses TypeScript, though? That's pretty much a direct superset of ES6. Uglifying works fine out of the box last time I tried, since the TypeScript gets compiled away before it reaches that point. Commented May 11, 2017 at 15:35
  • I use Set and Map in angular 4 projects - no problems. Commented May 11, 2017 at 16:05
  • @Julia Even with a production build? Do you have target set to es6? Commented May 11, 2017 at 16:19
  • yes it prod. target is es5. Angular itself use a lot of Map. Commented May 11, 2017 at 16:27

1 Answer 1

2

Typescript can't transpile ES6 code to ES5

This isn't the case. (And that doesn't really make sense, as Typescript is just the language and the transcompilation is separate).

Set the build target to ES5. If you're writing in Typescript, then you have access to those ES6 features, as Typescript is a superset of the latest version of ECMAScript (theoretically).

If you're getting issues, it may have to do with your module. Try commonjs or similar to get access to a module that can transpile to ES5.

Sign up to request clarification or add additional context in comments.

3 Comments

Setting target to es5 (and leaving modules as es6) worked -- after I removed certain es6-specific code such as use of the spread operator, Array.includes, etc. This is the probably the best I can do without adding Babel into the mix.
For more information, it looks like they decided to allow that combination sometime last year, putting the onus on the developer to take care of any incompatibilities. github.com/Microsoft/TypeScript/issues/6319
With Typescript, and module: "es2015", I have had no issues in using ES6-specific code in my angular-cli project. except the things that don't comply with Angular's AOT Compiler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.