什么是 tsconfig.json

概述

¥Overview

目录中存在 tsconfig.json 文件表明该目录是 TypeScript 项目的根目录。tsconfig.json 文件指定编译项目所需的根文件和编译器选项。

¥The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.

JavaScript 项目可以改用 jsconfig.json 文件,它的作用几乎相同,但默认启用了一些与 JavaScript 相关的编译器标志。

¥JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

项目通过以下方式之一编译:

¥A project is compiled in one of the following ways:

使用 tsconfig.jsonjsconfig.json

¥Using tsconfig.json or jsconfig.json

  • 通过在没有输入文件的情况下调用 tsc,在这种情况下,编译器会从当前目录开始搜索 tsconfig.json 文件,并继续沿父目录链向上。

    ¥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.

  • 通过在没有输入文件和 --project(或只是 -p)命令行选项的情况下调用 tsc,该选项指定包含 tsconfig.json 文件的目录的路径,或包含配置的有效 .json 文件的路径。

    ¥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.

在命令行上指定输入文件时,tsconfig.json 文件将被忽略。

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

示例

¥Examples

示例 tsconfig.json 文件:

¥Example tsconfig.json files:

TSConfig 基础

¥TSConfig Bases

根据你打算在其中运行代码的 JavaScript 运行时环境,可能有一个你可以在 github.com/tsconfig/bases 使用的基本配置。这些是你的项目扩展的 tsconfig.json 文件,通过处理运行时支持来简化你的 tsconfig.json

¥Depending on the JavaScript runtime environment which you intend to run your code in, there may be a base configuration which you can use at github.com/tsconfig/bases. These are tsconfig.json files which your project extends from which simplifies your tsconfig.json by handling the runtime support.

例如,如果你正在编写一个使用 Node.js 版本 12 及更高版本的项目,那么你可以使用 npm 模块 @tsconfig/node12

¥For example, if you were writing a project which uses Node.js version 12 and above, then you could use the npm module @tsconfig/node12:

{
"": "@tsconfig/node12/tsconfig.json",
},
"": ["src/**/*"],
"": ["**/*.spec.ts"]
}

这让你的 tsconfig.json 专注于你项目的独特选择,而不是所有运行时机制。已经有一些 tsconfig 基础,我们希望社区可以为不同的环境添加更多。

¥This lets your tsconfig.json focus on the unique choices for your project, and not all of the runtime mechanics. There are a few tsconfig bases already, and we’re hoping the community can add more for different environments.

详情

¥Details

"compilerOptions" 属性可以省略,在这种情况下使用编译器的默认值。请参阅我们支持的 编译器选项 的完整列表。

¥The "compilerOptions" property can be omitted, in which case the compiler’s defaults are used. See our full list of supported Compiler Options.

TSConfig 参考手册

¥TSConfig Reference

去了解有关 TSConfig 参考手册 中数百个配置选项的更多信息。

¥To learn more about the hundreds of configuration options in the TSConfig Reference.

模式

¥Schema

tsconfig.json Schema 可以在 JSON 模式仓库 找到。

¥The tsconfig.json Schema can be found at the JSON Schema Store.