TypeScript 词条目录
前端Wiki/词条/tsconfig.json 配置

tsconfig.json 配置

定义TypeScript 项目的编译配置文件:规定编译目标、模块方案、目录范围与严格级别

tsc 编译器所有行为都由 tsconfig.json 驱动。核心字段:target(编译到哪个 JS 版本,决定是否降级箭头函数/const 等)、module(输出 CommonJS 还是 ESM)、strict(开启全部严格检查)、jsx(编译 JSX 的方式,React 项目核心项)、include/exclude(参与编译的文件范围)。

最常踩的坑:noImplicitAny(强制参数标注类型)与 strictNullChecks(null/undefined 不能随便赋值)在 strict 模式下默认开启,从宽松项目迁移时会冒出大量报错。还有 paths(路径别名 @/* 映射)方便导入,但打包工具(webpack/vite)也要同步配置 alias,否则编译过、运行找不到模块。

TypeScript
{
  "compilerOptions": {
    "target": "ES2020",          // 编译到 ES2020 语法
    "module": "ESNext",          // 输出 ESM(配合打包工具)
    "moduleResolution": "bundler",
    "strict": true,              // ★ 新项目必开
    "jsx": "preserve",           // React: 保留 jsx 交给打包器
    "esModuleInterop": true,     // 允许 default 导入 CJS 模块
    "skipLibCheck": true,        // 跳过 .d.ts 检查加速编译
    "baseUrl": ".",              // 路径别名基准
    "paths": { "@/*": ["src/*"] },
    "noEmit": true,              // 只做类型检查,产物交给 Vite/webpack
    "lib": ["ES2020", "DOM"]
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["node_modules", "dist"]
}
⚠ 常见误区
🔗 相关词条声明文件 .d.tsBabel 做了什么Vite 与 esbuild
← 返回来源章节tsconfig 常用配置