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

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

相关推荐
哆啦A梦15882 小时前
Vue3魔法手册 作者 张天禹 07_标签
前端·vue.js·typescript
似水流年QC2 小时前
前端性能优化实战:用 IntersectionObserver 实现图片懒加载
前端·性能优化
q1cheng2 小时前
基于Spring Boot + Vue项目online_learn的权限控制机制分析
前端
扶苏10022 小时前
深入理解 Vue 3 的 watch
前端·javascript·vue.js
前端 贾公子2 小时前
组件 v-model 的封装实现原理及 Input 组件的核心实现(上)
服务器·前端·javascript
老骥伏枥~2 小时前
基于Spring Boot + Vue.js的图书管理系统
vue.js·spring boot·后端
weixin199701080162 小时前
亚马逊商品详情页前端性能优化实战
前端·性能优化
全栈前端老曹2 小时前
【Redis】 监控与慢查询日志 —— slowlog、INFO 命令、RedisInsight 可视化监控
前端·数据库·redis·缓存·全栈·数据库监控·slowlog
扶苏10022 小时前
Vue 3 的组合式 API(Composition API)优势
前端·javascript·vue.js