Full explanation of TSConfig configuration options
TSConfig is a configuration file for the TypeScript compiler, used to specify the behavior of the TypeScript compiler. TSConfig is stored in JSON format and supports many configuration options, including compiler options, file listings, and TSConfig inheritance, among others.
compilerOptions
compilerOptions
is one of the most commonly used options in TSConfig to specify the behavior of the TypeScript compiler. The following are common compilerOptions
configuration options:
target
: The target JavaScript version to compile the code against. The default is ES3. Possible values include ES3, ES5, ES6/ES2015, ES2016, ES2017, ES2018, ES2019, ES2020, ES2021, ESNext.module
: A module system for code generation. Defaults to CommonJS. Possible values include CommonJS, AMD, System, UMD, ES2015, ES2020, ESNext.lib
: Library files that need to be imported during compilation. By default, the compiler automatically includes the library files corresponding to the target version. If it needs to be specified manually, it can be set to an array containing the names of the libraries to be imported.allowJs
: Allows compilation of JavaScript files. The default is false.checkJs
: Check types when compiling JavaScript files. The default is false.
In addition to the options listed above, there are many other optional options. For example, here are some other common options:
declaration
: Generate.d.ts
declaration file. The default is false.sourceMap
: Generate.map
a file for debugging. The default is false.outDir
: Directory for output files. Defaults to the current directory.rootDir
: The root directory of the source code. Defaults to the current directory.strict
: Enables all strict type checking options. The default is false.
files
files
is an option that specifies a list of files. Only files listed in this list will be processed by the compiler. Here is an example of an files
option :
{
"files": [
"app.ts",
"helpers.ts"
]
}
In this example, only app.ts
and helpers.ts
will be processed by the compiler.
include 和 exclude
include
and exclude
are options that specify a file matching pattern. Using these options, it is possible to specify which files the compiler should compile and which files should be ignored. Here include
is exclude
an example of the and options:
{
"include": [
"src//*"
],
"exclude": [
"node_modules",
"/*.test.ts"
]
}
In this example, include
the pattern matches all .ts
and .tsx
. exclude
The pattern ignores node_modules
the directory and all .test.ts
files ending in .
extends
extends
It is an advanced option of TSConfig, which allows you to inherit the options of another TSConfig file in one TSConfig file. Here is an example of an extends
option :
{
"extends": "./base.json",
"compilerOptions": {
"outDir": "./dist"
}
}
In this example, the current TSConfig file inherits all the options in ./base.json
the file and overrides the optionscompilerOptions
in the .outDir
References
references
Options is the project ref system in TypeScript. It allows you to share code and type definitions between multiple TypeScript projects. Here is an example of an references
option :
{
"references": [
{ "path": "../shared-types" },
{ "path": "../shared-code" }
]
}
In this example, references
the options reference two different TypeScript projects, and the code and type definitions from those two projects can be used in the current project.
Conclusion
This article details the various options and usage of TSConfig. Understanding these options and usage can help you better configure the TypeScript compiler and better manage your TypeScript projects. If you want to learn more about TypeScript, check out the official TypeScript documentation.
Author: Liu Tao