A TypeScript configuration file allows us to specify several more options. We may specify several input directories instead of just one, we may specify paths/files to be excluded from compilation, or we may specify several more compilations flags.
If you want Visual Studio to invoke the TypeScript compiler automatically, you must give the project TypeScript configuration file its default name, which, tsconfig.json. Otherwise, you must invoke the TypeScript compiler manually and pass it the configuration file name as a command-line parameter.
In order to add a TypeScript configuration file to your project, right-click on the project icon in the project explorer and select Add New Item:
Then select TypeScript JSON Configuration File. The configuration file should appear in the project root. Replace its content with the following code:
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"outDir": "wwwroot/js"
},
"include": [
"wwwroot/ts/**/*.ts"
],
"exclude": [
"**/node_modules"
]
}
compileOnSave enables compilation on save, while compilerOptions contains all compiler flags. It is easy to recognize the same options we discussed previously:
- noEmitOnError: No code generation in the case of errors.
- removeComments: It removes/keeps comments in JavaScript-emitted code.
- sourceMap: It enables/disables map files.
- target: It specifies the JavaScript version of the emitted code; in our case, ECMAScript 5.
- outDir: It specifies where to place the emitted files. If omitted, this is next to their sources.
- noImplicitAny: It will be discussed later on in this chapter.
Intellisense suggests the allowed options and values, so there is no need to remember their exact names.
include specifies a list of directory or file patterns to include in the compilation. Each entry is a pattern that may match several directories/files, since entries may contain wildcards: * matches a single name, while ** matches any path. In our case, there is a single pattern that selects all files contained in wwwroot/ts and in all its subdirectories (because of the **), whose extension is .ts.
exclude specifies a list of patterns to remove from the ones added within other options (for instance, with the include option). In our case, a unique pattern excludes all files contained in any directory called node_modules located at any depth, since these directories usually include JavaScript packages downloaded with the NPM Package Manager.
There is also a files option to add a list of files (just files, not patterns) to the compilation:
"files":[
"file1.ts",
"dir/file2.ts",
...
]
If no files or include options are specified, the whole project root is taken.
Save tsconfig.json and modify tests.ts. When the file is saved, it should be compiled according to the options contained in tsconfig.json.
Once a tsconfig.json has been added, all options in the TypeScript Build panel of the project properties are disabled:
You may use this panel just to select the TypeScript version; that is, the compiler that is automatically invoked by Visual Studio. All other options must be specified in the configuration file you added.