tsconfig 配置

tsconfig.json(或 jsconfig.json)驱动 tsc 与编辑器:包含哪些文件转译到何种 JS类型检查严格度路径别名等。选项以官方 compilerOptions 文档为准,此处列学习与排错最常碰到的项。


快速严格的起点

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "verbatimModuleSyntax": true,
    "noUncheckedIndexedAccess": true
  }
}

target / 模块 组合需与 运行时与打包器 对齐;前端项目常见 module: ESNext + bundler 解析。


严格族(strict 打开后)

strictNullChecksstrictFunctionTypesnoImplicitAny 等。与 基础类型类型推断与类型守卫直接相关。


moduleResolution

  • node10(旧):CommonJS 时代习惯。
  • node16 / nodenext:贴近 Node ESM + exports
  • bundler:假设打包器做解析,适合 Vite/webpack。

影响 import 路径是否要写扩展名package.json types 解析等。


路径与工程引用

  • baseUrl + paths:符号链接与打包器 alias 需同时配置,避免「编辑器能过、tsc 不能」或相反。
  • composite + references:monorepo 子项目增量构建。

模块与命名空间


声明与类型根

  • types:限制自动纳入的 @types 包。
  • typeRoots:自定义声明目录。

与装饰器 / 类字段

  • experimentalDecorators / emitDecoratorMetadata:旧装饰器。
  • useDefineForClassFields:对齐 ECMAScript 类字段语义;影响 装饰器 行为。

检查而不 emit

  • noEmit:只做类型检查(常与 Vite、esbuild 等分工)。
  • declaration / emitDeclarationOnly:仅产出 .d.ts

TypeScript 基础目录索引

主题笔记
类型原语、unknown基础类型
interface / type接口与类型别名
泛型约束泛型
implements、可见性
窄化、is类型推断与类型守卫
enum枚举
工具类型、条件类型高级类型
ESM、namespace模块与命名空间
@ 装饰器装饰器

相关链接