×
☰ See All Chapters

Typescript tsconfig.json

tsc command accepts instructions in the form of command-line arguments, below is an example to set --watch option to automatically compile a TypeScript file when changes are made:

tsc main.ts --watch

tsc accepts a wide range of command-line arguments, but there's usually no need to learn them. Most developers set compiler options in a file named tsconfig.json. If tsc is executed in a directory containing tsconfig.json, it will read the file's settings and use them to configure its compilation. As its suffix implies, tsconfig.json defines a JSON object.

Compiler options specified on the command line override those specified in the tsconfig.json file, so compiler options take precedence over tsconfig.json

The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project.

A tsconfig.json file is permitted to be completely empty, which compiles all files included by default with the default compiler options.

How to use tsconfig.json

We can execute TypeScript project by using tsconfig.json by any one of following ways:

  1. By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json file starting in the current directory and continuing up the parent directory chain. 

  2. By invoking tsc with no input files and a --project (or just -p) command line option that specifies the path of a directory containing a tsconfig.json file, or a path to a valid .json file containing the configurations. 

  3. When input files are specified on the command line, tsconfig.json files are ignored. 

tsconfig.json example

Below is an example for tsconfig.json with certain configurations:

{

        "compilerOptions": {

                "module": "system",

                "noImplicitAny": true,

                "removeComments": true,

                "preserveConstEnums": true,

                "outFile": "../../built/local/tsc.js",

                "sourceMap": true,

                "typeRoots": [],

                "types": [],

                "outDir": "typeScript/ts/"

        },

        "include": [

                "src/**/*"

        ],

        "exclude": [

                "node_modules",

                "**/*.spec.ts"

        ],

        "files": [

                "core.ts",

                "sys.ts",

                "types.ts",

                "scanner.ts"

        ],

        "extends": "./configs/base",

        "compileOnSave": false,

        "typeAcquisition": {

                "enable": true,

                "exclude": [],

                "include": [

                        "lodash"

                ]

        }

}

To see full list of supported Compiler Options visit https://www.typescriptlang.org.


All Chapters
Author