什么是 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 文件,并向上查找父目录链。
  • 通过在没有输入文件的情况下调用 tsc,并使用 --project(或者仅使用 -p)命令行选项指定包含 tsconfig.json 文件的目录路径,或指定包含配置的有效 .json 文件的路径。

当在命令行上指定输入文件时,会忽略 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 架构可以在 JSON Schema Store 找到。

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