tsconfig.json完整使用配置介绍和示例

tsconfig.json 是 TypeScript 项目的核心配置文件 ,用于控制编译行为、类型检查规则、模块解析策略、文件包含/排除范围等。下面为你提供一份 完整、系统、实用的 tsconfig.json 使用指南,包括:

  • 配置结构说明
  • 常用字段详解
  • 不同场景(通用项目、Vue + TS、库开发)的配置示例
  • 最佳实践建议

一、基本结构

一个典型的 tsconfig.json 结构如下:

复制代码
{
  "compilerOptions": {
    // 编译选项(最重要)
  },
  "include": [],        // 包含哪些文件
  "exclude": [],        // 排除哪些文件
  "files": [],          // 显式指定文件(优先级高于 include)
  "extends": "",        // 继承其他配置
  "references": []      // 项目引用(monorepo)
}

⚠️ 注意:只要目录中存在 tsconfig.json,TypeScript 就认为这是项目根目录。


二、核心字段详解

1. compilerOptions(编译选项)

基础设置
配置项 说明 推荐值
target 编译成哪个 JS 版本(ES5/ES2015/ES2020/ESNext) "ES2020""ESNext"
module 模块系统(CommonJS/ESNext/AMD 等) "ESNext"(现代前端)或 "CommonJS"(Node.js)
moduleResolution 模块解析策略 "Node"(兼容 npm)
outDir 输出目录 "./dist"
rootDir 源码根目录 "./src"
sourceMap 生成 .map 文件便于调试 true
declaration 生成 .d.ts 声明文件 true(库开发需要)
removeComments 移除注释 false(开发时保留)
严格类型检查(强烈建议开启)
配置项 作用
strict 启用所有严格检查(相当于开启下面所有)✅
noImplicitAny 禁止隐式 any
strictNullChecks 严格 null/undefined 检查
strictFunctionTypes 严格函数参数检查
noImplicitThis 禁止隐式 this 类型
alwaysStrict 自动添加 "use strict"
兼容性 & 工程化
配置项 说明
esModuleInterop 允许 import React from 'react'(兼容 CommonJS)✅
allowSyntheticDefaultImports 允许从无默认导出的模块默认导入(配合 esModuleInterop
resolveJsonModule 支持 import data from './data.json'
skipLibCheck 跳过 node_modules/@types 的类型检查(提升速度)✅
forceConsistentCasingInFileNames 强制文件名大小写一致(避免跨平台问题)✅
allowJs 是否编译 .js 文件(混合项目)
checkJs .js 中报告类型错误(需 allowJs: true
高级优化
配置项 说明
incremental 启用增量编译(大幅提升二次构建速度)✅
tsBuildInfoFile 增量编译缓存文件路径(如 "./dist/.tsbuildinfo"
isolatedModules 强制每个文件可独立编译(与 Babel/Vite 兼容)✅
verbatimModuleSyntax 替代 importsNotUsedAsValues,更清晰控制 import 类型(TS 5.0+)✅

2. include / exclude / files

  • include: Glob 模式,包含要编译的文件(默认:["**/*"]

    "include": ["src//*", "types//*.d.ts"]

  • exclude: 排除文件(默认排除 node_modules, bower_components, jspm_packages, outDir

    "exclude": ["node_modules", "dist", "coverage"]

  • files: 显式列出文件(一旦使用,include 只对依赖生效)

✅ Vue 项目通常需包含 .vue 文件:


3. extends

继承其他配置,避免重复:

复制代码
{
  "extends": "@vue/tsconfig/tsconfig.dom.json",
  "compilerOptions": {
    // 覆盖或补充配置
  }
}

常用继承源:

  • @vue/tsconfig(Vue 官方)
  • tsconfig/base.json(自建基础配置)

三、典型配置示例

✅ 示例 1:通用前端项目(Vite + TS)

复制代码
{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "Node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "types": ["vite/client", "node"],
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "verbatimModuleSyntax": true,
    "noEmit": true
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.d.ts",
    "src/**/*.tsx",
    "src/**/*.vue"
  ],
  "exclude": ["node_modules", "dist"]
}

💡 noEmit: true 表示只做类型检查,不输出 JS(由 Vite/Bundler 负责编译)


✅ 示例 2:Node.js 后端项目

复制代码
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "sourceMap": true,
    "declaration": false,
    "incremental": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

✅ 示例 3:发布 NPM 包(带声明文件)

复制代码
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "Node",
    "outDir": "./dist",
    "declaration": true,
    "declarationDir": "./dist/types",
    "declarationMap": true,
    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

四、最佳实践建议

  1. **始终开启 **strict: true ------ 提升代码健壮性。
  2. **使用 include 而非 **files ------ 更灵活。
  3. **前端项目设 **noEmit: true ------ 交由构建工具处理。
  4. **启用 **incremental ------ 加快本地开发编译速度。
  5. **Vue 项目继承 **@vue/tsconfig ------ 避免踩坑。
  6. **不要手动维护 **lib ------ 除非有特殊需求(如 Web Worker)。
  7. 使用 paths 简化 import 路径(配合构建工具 alias)。

五、如何生成初始配置?

在项目根目录运行:

复制代码
npx tsc --init

会生成带详细注释的默认配置,按需

相关推荐
Highcharts.js8 小时前
Highcharts 云端渲染的真相:交互式图表与服务器端生成的边界
前端·信息可视化·服务器渲染·highcharts·图表渲染
zhuyan1089 小时前
Linux 系统磁盘爆满导致无法启动修复指南
前端·chrome
编程牛马姐10 小时前
独立站SEO流量增长:提高Google排名的优化方法
前端·javascript·网络
NotFound48610 小时前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
Dontla10 小时前
高基数(High Cardinality)问题介绍(Prometheus、高基数字段、低基数字段)
前端·数据库·prometheus
一 乐11 小时前
医院挂号|基于springboot + vue医院挂号管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·医院挂号管理系统
whuhewei12 小时前
为什么客户端不存在跨域问题
前端·安全
妮妮喔妮13 小时前
supabase的webhook报错
开发语言·前端·javascript
yivifu13 小时前
手搓HTML双行夹批效果
前端·html·html双行夹注
奔跑的卡卡14 小时前
Web开发与AI融合-第一篇:Web开发与AI融合的时代序幕
前端·人工智能