Roka545 Roka545 - 2 months ago 8
TypeScript Question

Is it possible to have multiple targets in compilerOptions?

Sorry if this is a nobb questions, but I'm building an Angular app and my current tsconfig.json file has 'es6' as the 'target' in 'compilerOptions':

{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"types" : []
},
"exclude": [
"node_modules"
]
}


Is it possible to have multiple target? For example, could I add in es5? I ask simply because I'm going through tutorials that are written for es5, but due to the current state of my project I need to use es6 - i.e., I cannot downgrade. All of my code has to conform to es6 - if both were included I would imagine that my code could conform to both es5 and es6.

Answer Source

You can only specify one target for the configuration. The target property of compilerOptions accepts a single string value.

Specifying ES5 as the target would not restrict you from writing your source in ES6 or higher, it simply ensures that the compiled code is compatible with clients that only understand ES5 or lower. Features such as lambda expressions will be compiled to function expressions.

An expression such as const foo = (bar) => bar.replace('\t', ''); would be compiled to var foo = function (bar) { return bar.replace('\t', ''); }; with target set to ES5.

Where you need the target to be compiled to ES5 or ES6 would really depend on what devices/browsers will be using your application, ES5 would definitely at this time be a more "safe" choice in terms of compatibility. Here is a nice resource for determining ES6 browser comparability.

Hopefully that helps clarify things!