TSConfig full solution

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

compilerOptionsis one of the most commonly used options in TSConfig to specify the behavior of the TypeScript compiler. The following are common compilerOptionsconfiguration 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.tsdeclaration file. The default is false.
  • sourceMap: Generate .mapa 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

filesis 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 filesoption :

{
  "files": [
    "app.ts",
    "helpers.ts"
  ]
}

In this example, only app.tsand helpers.tswill be processed by the compiler.

include 和 exclude

includeand excludeare 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 includeis excludean example of the and options:

{
  "include": [
    "src//*"
  ],
  "exclude": [
    "node_modules",
    "/*.test.ts"
  ]
}

In this example, includethe pattern matches all .tsand .tsx. excludeThe pattern ignores node_modulesthe directory and all .test.tsfiles ending in .

extends

extendsIt 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 extendsoption :

{
  "extends": "./base.json",
  "compilerOptions": {
    "outDir": "./dist"
  }
}

In this example, the current TSConfig file inherits all the options in ./base.jsonthe file and overrides the optionscompilerOptions in the .outDir

References

referencesOptions 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 referencesoption :

{
  "references": [
    { "path": "../shared-types" },
    { "path": "../shared-code" }
  ]
}

In this example, referencesthe 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

Guess you like

Origin blog.csdn.net/ekcchina/article/details/130300475