news 2026/5/28 10:27:15

15. tsconfig.json 配置详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
15. tsconfig.json 配置详解

15. tsconfig.json 配置详解

1. 概述

tsconfig.json是 TypeScript 项目的核心配置文件,用于指定编译选项、文件包含/排除规则、项目引用等。正确配置 tsconfig.json 是 TypeScript 项目工程化的基础。

┌─────────────────────────────────────────────────────────────┐ │ tsconfig.json 配置体系 │ ├─────────────────────────────────────────────────────────────┤ │ │ │ 顶层配置 │ │ ├── compilerOptions:编译选项 │ │ ├── include:包含的文件模式 │ │ ├── exclude:排除的文件模式 │ │ ├── files:显式指定的文件列表 │ │ ├──extends:继承其他配置文件 │ │ ├── references:项目引用 │ │ └── watchOptions:监听选项 │ │ │ │ 编译选项分类 │ │ ├── 基本选项:target、module、lib │ │ ├── 严格模式:strict、noImplicitAny 等 │ │ ├── 模块解析:moduleResolution、paths、baseUrl │ │ ├── 输出控制:outDir、rootDir、declaration │ │ ├── 互操作性:esModuleInterop、allowSyntheticDefaultImports│ │ ├── 类型检查:noUnusedLocals、noUnusedParameters │ │ └── 源映射:sourceMap、inlineSourceMap │ │ │ └─────────────────────────────────────────────────────────────┘

2. 生成 tsconfig.json

# 生成默认配置tsc--init# 生成带注释的配置文件tsc--init--showConfig# 使用特定模板tsc--init--targetes2020--modulecommonjs

3. 顶层配置

3.1 基础结构

{"compilerOptions":{// 编译选项},"include":["src/**/*"],"exclude":["node_modules","dist"],"files":[],"extends":"./base.json","references":[],"watchOptions":{}}

3.2 include 和 exclude

{// 包含所有 src 下的 TypeScript 文件"include":["src/**/*"],// 排除 node_modules 和 dist 目录"exclude":["node_modules","dist","**/*.test.ts"],// 显式指定文件(优先级最高)"files":["src/index.ts","src/types/global.d.ts"]}

3.3 extends 继承

// base.json{"compilerOptions":{"strict":true,"target":"ES2020","module":"ESNext"}}// tsconfig.json{"extends":"./base.json","compilerOptions":{"outDir":"./dist","rootDir":"./src"},"include":["src/**/*"]}

3.4 watchOptions 监听选项

{"watchOptions":{// 监听文件变化的方式"watchFile":"useFsEvents","watchDirectory":"useFsEvents","fallbackPolling":"dynamicPriority","synchronousWatchDirectory":true,"excludeDirectories":["**/node_modules","**/dist"]}}

4. 基本编译选项

4.1 target(目标版本)

{"compilerOptions":{// 编译目标 JS 版本// 可选:ES3、ES5、ES6/ES2015、ES2016、ES2017、ES2018、ES2019、ES2020、ES2021、ES2022、ESNext"target":"ES2020"}}

4.2 module(模块系统)

{"compilerOptions":{// 模块系统// 可选:none、commonjs、amd、umd、system、es6/es2015、es2020、es2022、esnext、node16、nodenext"module":"commonjs",// Node.js 项目// "module": "ESNext", // 前端项目// 模块解析策略"moduleResolution":"node",// 输出目录"outDir":"./dist",// 源码目录"rootDir":"./src",// 基础路径"baseUrl":".",// 路径映射"paths":{"@/*":["src/*"],"@components/*":["src/components/*"]}}}

4.3 lib(库文件)

{"compilerOptions":{// 包含的库文件"lib":["ES2020","DOM","DOM.Iterable"]// 常用组合:// 前端项目:["ESNext", "DOM", "DOM.Iterable"]// Node.js:["ESNext"]// 仅 ES:["ES2020"]}}

5. 严格模式配置

5.1 strict 家族

{"compilerOptions":{// 开启所有严格检查(推荐)"strict":true,// 禁止隐式 any"noImplicitAny":true,// 严格 null 检查"strictNullChecks":true,// 严格函数类型"strictFunctionTypes":true,// 严格 bind/call/apply"strictBindCallApply":true,// 严格属性初始化"strictPropertyInitialization":true,// 禁止隐式 this"noImplicitThis":true,// catch 变量类型为 unknown"useUnknownInCatchVariables":true,// 始终严格检查"alwaysStrict":true}}

5.2 单独配置示例

{"compilerOptions":{// 不开启全局严格模式"strict":false,// 只开启部分检查"noImplicitAny":true,"strictNullChecks":true,"noImplicitThis":true}}

6. 输出控制

6.1 基本输出配置

{"compilerOptions":{// 输出目录"outDir":"./dist",// 源码目录"rootDir":"./src",// 单个输出文件"outFile":"./dist/bundle.js",// 删除注释"removeComments":true,// 不生成输出(只做类型检查)"noEmit":false,// 错误时仍输出"noEmitOnError":false,// 生成声明文件"declaration":true,// 声明文件输出目录"declarationDir":"./types",// 生成声明映射文件"declarationMap":true,// 生成源映射"sourceMap":true,// 内联源映射"inlineSourceMap":false,// 内联源码"inlineSources":true}}

6.2 增量编译

{"compilerOptions":{// 启用增量编译"incremental":true,// 增量编译信息文件"tsBuildInfoFile":"./.tsbuildinfo",// 诊断信息"diagnostics":true,// 扩展诊断信息"extendedDiagnostics":false}}

7. 模块解析配置

7.1 路径映射

{"compilerOptions":{// 基础路径"baseUrl":".",// 路径映射"paths":{"@/*":["src/*"],"@components/*":["src/components/*"],"@utils/*":["src/utils/*"],"@services/*":["src/services/*"],"@types/*":["src/types/*"]},// 允许默认导入"allowSyntheticDefaultImports":true,// ES 模块互操作"esModuleInterop":true,// 解析 JSON 模块"resolveJsonModule":true,// 允许导入 JS 文件"allowJs":true,// 检查 JS 文件类型"checkJs":false}}

7.2 模块解析策略

{"compilerOptions":{// 模块解析策略// classic:传统 TypeScript 解析// node:Node.js 解析// bundler:打包器解析(TS 5.0+)// nodenext:Node.js 最新解析"moduleResolution":"node",// 解析非相对模块的基础路径"baseUrl":".",// 路径映射"paths":{"*":["node_modules/*","src/types/*"]},// 根路径"rootDirs":["src","generated"],// 类型根路径"typeRoots":["./node_modules/@types","./src/types"],// 类型包名"types":["node","jest","express"]}}

8. 类型检查选项

8.1 代码质量检查

{"compilerOptions":{// 未使用的局部变量报错"noUnusedLocals":true,// 未使用的参数报错"noUnusedParameters":true,// 检查函数是否有隐含返回值"noImplicitReturns":true,// 检查 switch 语句是否遗漏 case"noFallthroughCasesInSwitch":true,// 检查未使用的标签"noUnusedLabels":true}}

8.2 其他检查选项

{"compilerOptions":{// 禁止使用 any"noImplicitAny":true,// 禁止使用 any[](需要明确类型)"noImplicitAny":true,// 强制文件名大小写一致"forceConsistentCasingInFileNames":true,// 跳过库类型检查"skipLibCheck":true,// 跳过类型检查"skipDefaultLibCheck":false}}

9. 项目引用

9.1 配置项目引用

// tsconfig.base.json(基础配置){"compilerOptions":{"composite":true,"declaration":true,"declarationMap":true,"incremental":true}}// tsconfig.json(根配置){"files":[],"references":[{"path":"./packages/core"},{"path":"./packages/utils"},{"path":"./packages/api"},{"path":"./apps/web"},{"path":"./apps/admin"}]}// packages/core/tsconfig.json{"extends":"../../tsconfig.base.json","compilerOptions":{"outDir":"./dist","rootDir":"./src"},"include":["src/**/*"]}

9.2 构建项目引用

# 构建所有项目tsc--build# 构建特定项目tsc--buildpackages/core# 清理构建tsc--build--clean# 强制构建tsc--build--force

10. 完整配置示例

10.1 Node.js 项目配置

{"compilerOptions":{// 基本配置"target":"ES2020","module":"commonjs","lib":["ES2020"],"outDir":"./dist","rootDir":"./src",// 严格模式"strict":true,"noImplicitAny":true,"strictNullChecks":true,// 模块解析"moduleResolution":"node","esModuleInterop":true,"allowSyntheticDefaultImports":true,"resolveJsonModule":true,// 输出"declaration":true,"declarationMap":true,"sourceMap":true,"removeComments":true,// 检查"noUnusedLocals":true,"noUnusedParameters":true,"noImplicitReturns":true,"forceConsistentCasingInFileNames":true,// 其他"skipLibCheck":true,"incremental":true},"include":["src/**/*"],"exclude":["node_modules","dist","**/*.test.ts"]}

10.2 React 项目配置

{"compilerOptions":{// 基本配置"target":"ES2020","module":"ESNext","lib":["ES2020","DOM","DOM.Iterable"],"jsx":"react-jsx","outDir":"./dist","rootDir":"./src",// 严格模式"strict":true,// 模块解析"moduleResolution":"bundler","allowSyntheticDefaultImports":true,"esModuleInterop":true,"resolveJsonModule":true,// 路径映射"baseUrl":".","paths":{"@/*":["src/*"],"@components/*":["src/components/*"],"@hooks/*":["src/hooks/*"],"@utils/*":["src/utils/*"]},// 输出"sourceMap":true,// 检查"noUnusedLocals":true,"noUnusedParameters":true,"forceConsistentCasingInFileNames":true,// 其他"skipLibCheck":true},"include":["src/**/*"],"exclude":["node_modules","dist"]}

10.3 库项目配置

{"compilerOptions":{// 基本配置"target":"ES2020","module":"ESNext","lib":["ES2020"],"declaration":true,"declarationMap":true,"outDir":"./dist","rootDir":"./src",// 严格模式"strict":true,// 模块解析"moduleResolution":"node","esModuleInterop":true,// 输出"sourceMap":true,"removeComments":false,// 检查"noUnusedLocals":true,"noUnusedParameters":true,"forceConsistentCasingInFileNames":true,// 其他"skipLibCheck":true},"include":["src/**/*"],"exclude":["node_modules","dist","**/*.test.ts"]}

11. 常用配置组合

项目类型targetmodulelib其他关键配置
Node.jsES2020commonjsES2020esModuleInterop
ReactES2020ESNextES2020, DOMjsx: react-jsx
VueES2020ESNextES2020, DOM使用 vue-tsc
ES2020ESNextES2020declaration: true
浏览器ES2020ESNextES2020, DOMmoduleResolution: bundler

12. 总结

配置分类关键选项用途
基本target, module, lib语言版本和运行环境
严格strict, noImplicitAny类型安全
输出outDir, declaration, sourceMap编译产物
模块moduleResolution, paths, baseUrl模块解析
检查noUnusedLocals, noImplicitReturns代码质量
项目references, composite多项目构建

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/22 0:44:40

3步掌握中兴光猫高级管理:zteOnu工具实战指南

3步掌握中兴光猫高级管理:zteOnu工具实战指南 【免费下载链接】zteOnu A tool that can open ZTE onu device factory mode 项目地址: https://gitcode.com/gh_mirrors/zt/zteOnu 中兴光猫破解工具zteOnu是一款专为网络管理员和技术爱好者设计的专业级中兴ON…

作者头像 李华
网站建设 2026/5/22 0:44:02

io_uring 不只是更快的 epoll——fixed file、registered buffer、链式SQE、SQPOLL 的 6 个高级能力

去年帮一个做网关中间件的团队把 epoll 替换成 io_uring,折腾了两周,压测跑出来的数字让所有人沉默了:QPS 从 32 万涨到 34 万,提升 6.3%——这还是在已经深度优化过 epoll 的代码路径上,换了个异步框架写了几千行代码,就换来这么点提升,性价比低得可笑。团队里有人直接…

作者头像 李华
网站建设 2026/5/22 0:43:12

毕业设计 深度学习车道线检测(源码+论文)

文章目录 0 前言1 项目运行效果2 课题背景3 卷积神经网络3.1卷积层3.2 池化层3.3 激活函数:3.4 全连接层3.5 使用tensorflow中keras模块实现卷积神经网络 4 YOLOV56 数据集处理7 模型训练8 最后 0 前言 🔥这两年开始毕业设计和毕业答辩的要求和难度不断…

作者头像 李华
网站建设 2026/5/22 0:16:17

Django 从 0 到 1 打造完整电商平台:个人中心与用户信息修改

IT策士 10余年一线大厂经验,专注 IT 思维、架构、职场进阶。我也会在其它平台持续发布最新文章,助你少走弯路。 大家好,我是IT策士。用户登录之后,第一件想做的事往往是看看自己的账号信息,或者改个昵称、换个密码。今…

作者头像 李华
网站建设 2026/5/22 0:14:17

知识竞赛裁判怎么当?评分标准与争议处理

知识竞赛裁判怎么当?评分标准与争议处理公平 专业 高效 守护竞赛的生命线🎯 一、裁判的角色与职责知识竞赛裁判是竞赛公平的守护者,不仅要掌握规则,还要具备快速判断和沟通能力。核心职责:📋 赛前熟悉题…

作者头像 李华