Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Compile Options: module vs target

Trying to have some basic understanding about module and target.

I would like to know the difference between module and target compile options in a typical tsconfig.json

 {     "compilerOptions": {         "module": "es6",         "sourceMap": true,         "target": "es6"     } } 

What happens if I provide the following options:

module: commonjs, target: es6

module: es6, target: commonjs

module: commonjs, target: commonjs

like image 413
user203687 Avatar asked Sep 14 '16 14:09

user203687


People also ask

What is target in TypeScript?

The target setting changes which JS features are downleveled and which are left intact. For example, an arrow function () => this will be turned into an equivalent function expression if target is ES5 or lower. Changing target also changes the default value of lib .

Does TSC use Tsconfig?

Running tsc locally will compile the closest project defined by a tsconfig. json , you can compile a set of TypeScript files by passing in a glob of files you want.

What is TSC in TypeScript?

The TypeScript Compiler (also known as "tsc") is the backbone of many bundlers, plugins, and tools that work with TypeScript. While we don't often invoke tsc directly, we do configure how tsc behaves inside of the bundlers that we use.

Where does TSC compile to?

TypeScript provides a command-line utility tsc that compiles (transpiles) TypeScript files ( . ts ) into JavaScript. However, the tsc compiler (short for TypeScript compiler) needs a JSON configuration file to look for TypeScript files in the project and generate valid output files at a correct location.


2 Answers

There are 2 different things. --target simply means which version of ECMAScript you're using to code. --module simply means which module system you're using such as commonjs for Node or ES module for all that supports it and what not.

like image 56
motss Avatar answered Sep 30 '22 01:09

motss


A more detailed explanation is here : Understanding "target" and "module" in tsconfig


See also: Understanding "target" and "module" in tsconfig.

Here is a quote from the documentation on compiler options:

--target

Specify ECMAScript target version: 'es3' (default), 'es5', or 'es6'.

--module

Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', or 'es2015'.

  • Only 'amd' and 'system' can be used in conjunction with --outFile.
  • 'es6' and 'es2015' values may be used when targeting ES5 or lower.
like image 34
Paleo Avatar answered Sep 30 '22 03:09

Paleo