Marius Schulz
Marius Schulz
Front End Engineer

Generating Default TypeScript Configuration Files with tsc --init

When you're setting up a new project that uses TypeScript, you're likely going to create a tsconfig.json file in which you specify the compiler options to use for compiling your project.

Here's an example of how a tsconfig.json could look like for the TypeScript compiler itself:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "out": "../../built/local/tsc.js",
    "sourceMap": true
  },
  "files": [
    "core.ts",
    "sys.ts",
    "types.ts",
    "scanner.ts",
    "parser.ts",
    "utilities.ts",
    "binder.ts",
    "checker.ts",
    "emitter.ts",
    "program.ts",
    "commandLineParser.ts",
    "tsc.ts",
    "diagnosticInformationMap.generated.ts"
  ]
}

However, creating this file manually or copying it from an existing project is cumbersome. For this reason, TypeScript 1.6 introduced the --init flag for the tsc executable. Running tsc --init will create a tsconfig.json in the current working directory, which can look like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es3",
    "noImplicitAny": false,
    "outDir": "built",
    "rootDir": ".",
    "sourceMap": false
  },
  "exclude": ["node_modules"]
}

The default properties are a reasonable starting point for your project. Note that the exclude property is used (rather than files), which causes the TypeScript compiler to include every TypeScript file in the containing directory and its subdirectories, except those listed within the exclude array. No need to list every *.ts file to include explicitly!

By default, ECMAScript 3 is specified as the language level targeted for transpilation. If your target environment supports ECMAScript 5 or even ECMAScript 6 (or higher), feel free to target ES5 or ES6 (or higher) instead. Similarly, change the values of the outDir, rootDir, and sourceMap properties to your liking.

Finally, if you want to turn it up to eleven and fully leverage TypeScript's type checking capabilities, set noImplicitAny to true to have the compiler complain about expressions or declarations implicitly typed as any. I recommend you do this for all your TypeScript projects.

And that's it! tsc --init, a little helper that makes it a little more pleasant to set up a fresh TypeScript project.