The compilation context refers to a collection of TypeScript files that are parsed and analyzed to assess their validity. This context includes details regarding the compiler options currently in effect. We can establish this logical set of TypeScript files by utilizing a tsconfig.json file.
The TypeScript files can be compiled utilizing the command tsc <file name>.ts. When executing the '$tsc' command for compiling TypeScript code, the compiler looks for configurations specified in the tsconfig.json file. Additionally, TypeScript offers a feature to compile several .ts files within a larger project.
tsconfig.json
The tsconfig.json file is a configuration file formatted in JSON. Within the tsconfig.json file, various settings can be defined to instruct the compiler on how to compile the present project.
The initial action when starting a new TypeScript project is to incorporate a tsconfig.json file. To generate this file, navigate to the directory where you intend to keep your source files and create a new file titled tsconfig.json.
There are several methods to compile a TypeScript project:
- By running tsc without any specified input files: Here, the compiler looks for the tsconfig.json file beginning in the current directory and continues up through the parent directories.
- By executing tsc without input files along with a --project (or -p) argument: In this scenario, the compiler indicates the directory path that holds a tsconfig.json file, as well as a path to a valid .json file containing the necessary configurations.
Create tsconfig.json file
We can generate a tsconfig.json file in the root directory of our project by executing the following command.
$ tsc --init
Executing the command above will generate a default tsconfig.json file.
{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": false,
"experimentalDecorators": false,
"module": "none",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": false,
"noImplicitAny": false,
"noImplicitReturns": false,
"removeComments": false,
"sourceMap": false,
"strictNullChecks": false,
"target": "es3"
},
"compileOnSave": true
}
Include and Exclude properties
The include and exclude properties enable us to specify an array pattern for adding or omitting a set of TypeScript files during the compilation process.
{
"compilerOptions": {
"module": "system",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "../../built/local/tsc.js",
"sourceMap": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
compilerOptions Property
We can customize the compiler options properties by using compilerOptions. It allows specifying additional options to the typescript compiler. Some of the important compiler options are summarized in the following table.
| Option | Type | Default | Description |
|---|---|---|---|
| --allowJs | boolean | false | Allow JavaScript files to be compiled. |
| --alwaysStrict | boolean | false | Parse in strict mode and emit "use strict" for each source file. |
| --baseUrl | string | It is base directory to resolve non-directory module names. | |
| --build--b | boolean | false | It is used to build a project and all of its dependencies specified by project references. |
| --declaration--d | boolean | false | It generates a corresponding .d.ts file. |
| --diagnostics | boolean | false | It shows diagnostic information. |
| --experimentalDecorators | boolean | false | It enables the experimental support for ES decorators. |
| --isolatedModules | boolean | false | It is used to transpile each file as a separate module. |
| --module--m | string | target === "ES3" or "ES5" ? "CommonJS" : "ES6" | The output module type, e.g. "CommonJS", ?AMD?, "System", "ES6", "ES2015" or "ESNext." Default value is CommonJS if the target attribute is ES3 or ES5; else default is ES6. |
| --moduleResolution | string | module === "AMD" or "System" or "ES6" ? "Classic" : "Node" | It determines how modules get resolved. Either "Node" for Node.js/io.js style resolution, or "Classic." |
| --noEmitOnError | boolean | false | Do not emit outputs if any errors were reported. |
| --outDir | string | Redirect output structure to the directory. | |
| --sourceMap | boolean | false | Generates a corresponding .map file. It helps in debugging. |
| --target--t | string | "ES3" | Specify ECMAScript target version: "ES3" (default), "ES5", "ES6"/"ES2015", "ES2016", "ES2017" or "ESNext". |
| --watch--w | It runs the compiler in watch mode. It means that whenever any of the source files are changed, then the compiling process is re-triggered to generate the transpiled files again. |
To see the complete list of compiler options go to the official page .
compileOnSave
This property is utilized to configure the IDE for compiling the associated TypeScript files and automatically producing the output. It instructs the IDE to generate all files specified in a tsconfig.json file whenever a save action occurs.
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny" : true
}
}