工程化与综合第 31 章预计 15 分钟
本章示例(4 个,可直接运行)

示例在隔离的 iframe 中执行,console 输出会显示在运行面板里;源码与测试文件可在源码页查看。

31 · config-matrix

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

31 · noemit-on-error

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

31 · isolated-verbatim

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

31 · rootdir-sourcemap

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

本页目录(18 节)

31 · tsconfig 深潜、类型检查与转译分离

学完这章,你能把 tsc 当成「类型检查器 + 语法降级器」而不是打包器,并用配置矩阵控制产物布局、严格度与兼容性。

本章目标

  • 能解释 target / lib / module / moduleResolution 各自影响什么
  • 能区分 noEmit 与 noEmitOnError,并知道默认值
  • 能用 isolatedModules / verbatimModuleSyntax / erasableSyntaxOnly 约束「单文件可转译」
  • 能显式设置 rootDir,理解 sourceMap 与 tsbuildinfo
  • 能读懂 TS5011、TS5110、TS1205、TS5102

前置知识

  • 第 01 章(tsc 流水线)、第 23/24 章(模块与解析)
  • 第 26 章(monorepo/project references)

从你熟悉的语言出发

你熟悉的 编译开关 TypeScript 的对应 关键差异
javac -source/-target、classpath target/lib、moduleResolution tsc 不做链接/打包
C/C++ 编译器 -std、include path、优化级别 target、paths、strict 系列 tsc 不生成机器码
Make/Ninja 增量构建、依赖图 tsc -b + .tsbuildinfo TS 的增量粒度是项目/文件
Babel/esbuild 只转译 tsc 的 emit 阶段 Babel 不做类型检查,tsc 两者都做

正文

31.1 tsc 到底做什么

parse → type check → emit(删除类型、按 target 降级语法、生成 map/声明)

它不做:

  • 打包(不合并模块)
  • tree-shaking
  • 资源处理(CSS/图片)
  • polyfill

所以现代项目常见组合是:

  • 开发/构建:Vite/esbuild/Rollup(只转译/打包)
  • CI 类型检查:tsc --noEmit
  • 库声明:tsc --emitDeclarationOnly

31.2 四个核心旋钮

选项 影响 不影响
target 产物语法(可选链、类字段等如何降级) API 是否存在(不做 polyfill)
lib 类型检查时可用的内置/DOM API 产物语法
module 产物模块格式(ES2022、commonjs、preserve…) 模块如何被加载
moduleResolution 如何解析 import 产物内容

矩阵里最容易踩的是 module 与 moduleResolution 的配对:

module: node16  ←→  moduleResolution: node16
module: commonjs ←→ moduleResolution: bundler(示例允许)
module: ES2022  ←→  moduleResolution: bundler

不匹配会报 TS5110:

tsconfig.fail-mismatch.json(4,15): error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

31.3 noEmit 与 noEmitOnError

选项 含义 默认
noEmit 永不产出,只检查 false
noEmitOnError 检查失败时不产出 false

noEmitOnError 默认 false 是历史行为:tsc 倾向于让你还能调试。CI 里应显式设为 true,否则错误代码会悄悄落盘。

示例 02-noemit-on-error 用两份配置对比:

  • dist-fail-strict/:不产出。
  • dist-fail-emit/:仍产出 demo/type-error.js。

31.4 单文件可转译的三件套

选项 作用 典型报错
isolatedModules 每个文件必须能独立转译 TS1205(类型重导出未用 export type)
verbatimModuleSyntax 模块语法原样保留,类型导入必须显式 TS1484、TS1205
erasableSyntaxOnly 禁止有产物的 TS 语法(enum/namespace/装饰器等) TS1294

这三个选项让 esbuild/SWC/Vite 可以「逐文件转译」,不需要全局类型信息。代价是写法更严格:

import type { Shape } from './types.js';
export type { Shape };

31.5 rootDir、outDir 与 sourceMap

TS 7 的 rootDir 默认是配置文件所在目录。不写会出现:

tsconfig.fail-noroot.json(4,5): error TS5011: The common source directory of 'tsconfig.fail-noroot.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.

显式写 "rootDir": "src" 后:

src/main.ts → dist/main.js
src/main.ts → dist/main.js.map(sourceMap: true)

sourceMap 让 DevTools 断点回到 .ts;inlineSources 把源码嵌进 map,便于不发布源码时调试。

31.6 增量与项目引用

  • incremental: true 生成 .tsbuildinfo,下次只检查变化的文件。
  • tsc -b 按 references 拓扑构建,每个 composite 包有自己的 .tsbuildinfo。
  • 不要把 .tsbuildinfo 提交到仓库(本仓库 .gitignore 已忽略)。

31.7 严格选项与 skipLibCheck

基线开启了 strict 与额外选项(exactOptionalPropertyTypes、noUncheckedIndexedAccess 等)。逐项开启的顺序见第 16 章。

skipLibCheck: true 会跳过 .d.ts 内部检查:

  • 好处:第三方声明冲突时能先跑起来。
  • 坏处:隐藏真正的类型冲突;升级依赖后可能突然暴露。
  • 本仓库基线保持 false;只在明确评估后局部覆盖。

31.8 TS 7 的并行与 API

TS 7 是 Go 原生实现,提供并行检查开关(--checkers、--builders、--singleThreaded)。本章不把它们写进基线——它们是性能调优项,不是语义项。

TS 7 不提供编译器 API:不要 import 'typescript' 做类型分析,脚本一律 spawn tsc。需要 API 的工具链(typescript-eslint、Volar)此时用 @typescript/typescript6 并排安装(本教程不这么做)。

类型错误现场

场景一:module 与解析不匹配。examples/24-module-resolution/02-extension-resolution/tsconfig.fail-mismatch.json:

tsconfig.fail-mismatch.json(4,15): error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.
  • TS5110 是配对约束。
  • 修复:把 module 改成 node16,或把解析改回 bundler。

场景二:类型重导出未加 type。examples/31-tsconfig-build-pipeline/03-isolated-verbatim/demo/reexport.ts:

demo/reexport.ts(3,10): error TS1484: 'Shape' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
demo/reexport.ts(5,10): error TS1205: Re-exporting a type when 'verbatimModuleSyntax' is enabled requires using 'export type'.
  • 修复:import type + export type。

场景三:缺少 rootDir:

tsconfig.fail-noroot.json(4,5): error TS5011: The common source directory of 'tsconfig.fail-noroot.json' is './src'. The 'rootDir' setting must be explicitly set to this or another path to adjust your output's file layout.
  • 修复:显式 "rootDir": "src"。

反直觉差异:noEmitOnError 默认不阻止产出

你的直觉:类型错误应该让构建失败,产物不应该落盘。

实际行为:noEmitOnError 默认是 false。即使 tsc 退出码非 0,JS 仍会被写出:

demo/type-error.ts(3,14): error TS2322: Type 'string' is not assignable to type 'number'.

但 dist/.../type-error.js 仍然存在。CI 若只看「文件生成了没有」,会误判成功。

为什么这样设计:早期 tsc 的定位是「尽量让你能运行/调试」,即使类型有错。随着 CI 普及,团队才普遍显式打开 noEmitOnError。

正确写法:

  • 基线/CI 显式 "noEmitOnError": true。
  • 检查退出码,而不是只检查文件存在。
  • 构建脚本把 tsc 的退出码传播出去。

示例索引

目录 演示什么 预期看到
examples/31-tsconfig-build-pipeline/01-config-matrix/ target 降级 vs lib 2、0;两份产物语法不同
examples/31-tsconfig-build-pipeline/02-noemit-on-error/ 错误时是否产出 严格配置无文件、宽松配置有文件
examples/31-tsconfig-build-pipeline/03-isolated-verbatim/ 单文件可转译 rect;失败配置贴 TS1484/TS1205/TS1294
examples/31-tsconfig-build-pipeline/04-rootdir-sourcemap/ rootDir 与 source map ok;失败配置贴 TS5011

练习题

  1. 把 01-config-matrix 的 target 改成 ES2017,观察 ?./?? 的降级差异。
  2. 给 02-noemit-on-error 增加一份 declaration: true 的配置,观察错误时是否生成 .d.ts。
  3. 给 03-isolated-verbatim 增加一个 namespace 示例,验证 erasableSyntaxOnly 的报错。
  1. ES2017 不支持 ??,会被降级成条件表达式;?. 也会被展开。
  2. noEmitOnError: true 时 .d.ts 也不生成;false 时会生成(即使类型有错)。
  3. export namespace N { export const x = 1; } 在 erasableSyntaxOnly 下报 TS1294。

章末自检

  1. target 会做 polyfill 吗?
  2. noEmit 与 noEmitOnError 的默认值分别是什么?
  3. isolatedModules 为什么要求 export type?
  4. rootDir 不写会发生什么?
  5. skipLibCheck 的取舍是什么?
  6. tsc 能代替打包器吗?
  1. 不会,只降语法。
  2. 都是 false(本仓库基线显式把 noEmitOnError 设为 true)。
  3. 单文件转译器无法知道一个名字是类型还是值,必须显式标注。
  4. TS 7 会报 TS5011;旧版本会产出多一层目录(dist/src/...)。
  5. 能绕过第三方声明冲突,但会隐藏真正的类型问题。
  6. 不能;它不合并模块、不做 tree-shaking/资源处理。

延伸阅读

  • TypeScript Handbook:tsconfig Reference(target、lib、module、moduleResolution、noEmitOnError)
  • TypeScript 5.0 Release Notes:verbatimModuleSyntax
  • TypeScript 5.8 Release Notes:--erasableSyntaxOnly
  • TypeScript 7.0 Release Notes(Go 移植、并行开关、无编译器 API)