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

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

相关推荐
vipbic3 小时前
别再把“做个H5”挂嘴边了:这个词,官方压根就没有定义过
前端
ZC跨境爬虫4 小时前
跟着 MDN 学CSS day_39:(Flexbox 弹性盒子核心机制)
前端·css·ui·html·tensorflow
小陈同学呦5 小时前
前端如何处理订单状态导航的数据竞态问题
前端·javascript
喵个咪5 小时前
GoWind Toolkit 前端代码生成|Vue3(ElementPlus/Vben)、React(AntDesign)全自动一键生成教程
前端·vue.js·react.js
qq_2518364576 小时前
SpringBoot+Vue 共享电池柜管理系统 完整实现 前后端分离项目实战 完整代码
vue.js·spring boot·后端
摆烂大大王6 小时前
玩转 OpenClaw:用 TaskFlow + Heartbeat 打造自动化工作流
前端·人工智能·自动化
zhangxingchao6 小时前
AI 大模型核心六:量化、Workflow 与 Agent、多轮 RAG
前端·人工智能·后端
梦想的颜色7 小时前
TypeScript 完全指南(上):从零开始掌握类型系统
前端·typescript
之歆7 小时前
Day01_ES6+ 专业指南:从基础到实战的现代JavaScript开发(下)
前端·javascript·es6
lichenyang4537 小时前
鸿蒙 MVVM 实战:从 Demo 到工程化,聊聊登录、状态管理与埋点系统设计
前端