857 words
4 minutes
TSConfig 编译器诊断配置
2025-02-27 10:32:16
2025-12-24 23:45:46

← 返回 TSConfig 参考指南


diagnostics#

用于输出用于调试的诊断信息。此命令是 extendedDiagnostics 的一个子集,提供更面向用户的结果,更易于理解。

如果 TypeScript 编译器工程师要求你在编译时提供此标志的结果,使用 extendedDiagnostics 替代也是没有问题的。

已废弃

相关配置:

  • extendedDiagnostics

发布版本:1.0

explainFiles#

打印 TypeScript 视为项目一部分的文件名称,以及它们成为编译内容的原因。

例如,对于这个只有一个 index.ts 文件的项目:

example
├── index.ts
├── package.json
└── tsconfig.json

使用设置了 explainFiles 为 true 的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "explainFiles": true
  }
}

在此文件夹中运行 TypeScript 将输出如下内容:

 tsc
node_modules/typescript/lib/lib.d.ts
  Default library for target 'es5'
node_modules/typescript/lib/lib.es5.d.ts
  Library referenced via 'es5' from file 'node_modules/typescript/lib/lib.d.ts'
node_modules/typescript/lib/lib.dom.d.ts
  Library referenced via 'dom' from file 'node_modules/typescript/lib/lib.d.ts'
node_modules/typescript/lib/lib.webworker.importscripts.d.ts
  Library referenced via 'webworker.importscripts' from
    file 'node_modules/typescript/lib/lib.d.ts'
node_modules/typescript/lib/lib.scripthost.d.ts
  Library referenced via 'scripthost'
    from file 'node_modules/typescript/lib/lib.d.ts'
index.ts
  Matched by include pattern '**/*' in 'tsconfig.json'

上述输出显示:

  • 基于目标的初始 lib.d.ts 查找,以及引用的 .d.ts 文件链
  • 通过 include 的默认模式定位的 index.ts 文件

此选项用于调试文件是如何成为编译内容的一部分的。

发布版本:4.2

extendedDiagnostics#

你可以使用此标志来发现 TypeScript 在编译时将时间花在了哪里。这是一个用于理解代码库整体性能特征的工具。

你可以在 wiki 的性能部分了解更多关于如何测量和理解输出的信息。

相关配置:

  • diagnostics

发布版本:2.0

generateCpuProfile#

此选项允许你在编译器运行期间生成 v8 CPU 分析文件。CPU 分析可以帮助了解构建可能变慢的原因。

此选项只能通过 CLI 使用:—generateCpuProfile tsc-output.cpuprofile。

npm run tsc --generateCpuProfile tsc-output.cpuprofile

此文件可以在基于 Chromium 的浏览器(如 Chrome 或 Edge Developer)的 CPU 分析器部分中打开。你可以在 TypeScript wiki 的性能部分了解更多关于理解编译器性能的信息。

默认值:profile.cpuprofile

发布版本:3.7

generateTrace#

生成事件跟踪和类型列表。

发布版本:4.1

listEmittedFiles#

打印编译过程中生成的文件名到终端。

此标志在以下两种情况下很有用:

  • 你想在终端中将 TypeScript 转译作为构建链的一部分,其中文件名会在下一个命令中被处理
  • 你不确定 TypeScript 是否包含了你期望的文件,可用于调试文件包含设置

例如:

example
├── index.ts
├── package.json
└── tsconfig.json

使用以下配置:

{
  "compilerOptions": {
    "declaration": true,
    "listEmittedFiles": true
  }
}

将会输出如下路径:

$ npm run tsc
path/to/example/index.js
path/to/example/index.d.ts

通常情况下,TypeScript 在成功时会静默返回。

发布版本:2.0

listFiles#

打印编译过程中包含的文件名。当你不确定 TypeScript 是否包含了你期望的文件时,这个选项很有用。

例如:

example
├── index.ts
├── package.json
└── tsconfig.json

使用以下配置:

{
  "compilerOptions": {
    "listFiles": true
  }
}

将会输出如下路径:

$ npm run tsc
path/to/example/node_modules/typescript/lib/lib.d.ts
path/to/example/node_modules/typescript/lib/lib.es5.d.ts
path/to/example/node_modules/typescript/lib/lib.dom.d.ts
path/to/example/node_modules/typescript/lib/lib.webworker.importscripts.d.ts
path/to/example/node_modules/typescript/lib/lib.scripthost.d.ts
path/to/example/index.ts

注意:如果使用 TypeScript 4.2 版本,建议使用 explainFiles,它还会提供文件被添加的原因说明。

相关配置:

  • explainFiles

发布版本:1.5

noCheck#

禁用完整的类型检查(只会报告关键的解析和生成错误)。

发布版本:5.6

traceResolution#

当你试图调试为什么某个模块没有被包含时,可以将 traceResolution 设置为 true,TypeScript 将打印出每个处理文件的解析过程信息。

发布版本:2.0

TSConfig 编译器诊断配置
https://0bipinnata0.my/posts/typescript/tsconfig/09-compiler-diagnostics/
Author
0bipinnata0
Published at
2025-02-27 10:32:16