跳转到内容

tsconfig

此内容尚不支持你的语言。

tsconfig.json 是 TypeScript 项目的配置文件, 用于指定编译选项和项目结构。

当运行 tsc 命令时, TypeScript 会按以下顺序查找配置文件:

  1. 当前目录的 tsconfig.json
  2. 当前目录的 tsconfig.json5
  3. 父目录的 tsconfig.json
  4. 直到文件系统根目录

也可以通过 --project-p 参数指定配置文件:

Terminal window
tsc -p ./path/to/tsconfig.json
{
"compilerOptions": {
/* 编译选项 */
},
"files": [
/* 指定编译的文件列表 */
],
"include": [
/* 指定编译的文件/目录 glob 模式 */
],
"exclude": [
/* 指定排除的文件/目录 glob 模式 */
],
"extends": [
/* 继承的配置文件 */
],
"references": [
/* 项目引用 */
]
}
配置项描述
files显式指定要编译的文件列表
include指定要编译的文件/目录, 支持 glob 模式
exclude指定要排除的文件/目录, 支持 glob 模式
{
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.test.ts"]
}

继承其他配置文件:

{
"extends": "@tsconfig/node20/tsconfig.json"
}
配置项描述默认值
target编译后的 JavaScript 版本ES3
module模块系统取决于 target
lib编译时包含的库默认值取决于 target
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"lib": ["ES2022", "DOM"]
}
}

常见的 target 值:

  • ES3 - 最老的版本, 兼容性最好
  • ES5 - 支持 IE9+
  • ES2015 / ES6
  • ES2016 - ES7
  • ES2017 - ES8
  • ESNext - 最新特性

常见的 module 值:

  • CommonJS - Node.js 默认
  • ES6 / ES2015 - ES 模块
  • AMD - 异步模块定义
  • System - SystemJS
  • UMD - 通用模块定义
  • NodeNext - Node.js 12+ 支持的模块解析
  • Preserve - 保留原始模块语法
配置项描述
moduleResolution模块解析策略
baseUrl解析非相对路径的基础目录
paths路径映射
rootDirs组合多个目录为一个虚拟目录
typeRoots类型定义文件的目录
types自动包含的类型定义包
{
"compilerOptions": {
"moduleResolution": "Bundler",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@utils/*": ["src/utils/*"]
}
}
}
配置项描述
strict启用所有严格类型检查选项
noImplicitAny不允许隐式的 any 类型
strictNullChecks严格的 null/undefined 检查
strictFunctionTypes严格的函数参数类型检查
strictBindCallApply严格的 bind/call/apply 检查
strictPropertyInitialization严格的属性初始化检查
noImplicitThis不允许隐式的 this 类型
useUnknownInCatchVariablescatch 变量使用 unknown 类型
{
"compilerOptions": {
"strict": true
}
}
配置项描述
noUnusedLocals不允许未使用的局部变量
noUnusedParameters不允许未使用的函数参数
noImplicitReturns函数所有路径都必须有返回值
noFallthroughCasesInSwitch不允许 switch case 贯穿
noUncheckedIndexedAccess索引访问结果添加 undefined
noPropertyAccessFromIndexSignature只能通过索引访问的属性不能使用点访问
{
"compilerOptions": {
"noUnusedLocals": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
}
}
配置项描述
outDir输出目录
outFile合并输出到单个文件
rootDir输入文件的根目录
removeComments移除注释
noEmit不生成输出文件(只做类型检查)
declaration生成 .d.ts 声明文件
declarationMap生成声明文件的 source map
sourceMap生成 source map
inlineSourceMap内联 source map
esModuleInterop启用 ES 模块互操作性
allowSyntheticDefaultImports允许从没有默认导出的模块默认导入
{
"compilerOptions": {
"outDir": "./dist",
"declaration": true,
"sourceMap": true,
"removeComments": true
}
}
配置项描述
esModuleInterop启用 ES 模块和 CommonJS 的互操作性
allowSyntheticDefaultImports允许从没有默认导出的模块默认导入
isolatedModules将每个文件作为单独的模块
verbatimModuleSyntax保持模块语法的原样
{
"compilerOptions": {
"esModuleInterop": true,
"isolatedModules": true
}
}
配置项描述
allowJs允许编译 JavaScript 文件
checkJs检查 JavaScript 文件的类型
jsxJSX 编译方式
experimentalDecorators启用装饰器
emitDecoratorMetadata为装饰器发射元数据
skipLibCheck跳过声明文件的类型检查
forceConsistentCasingInFileNames强制文件名大小写一致
resolveJsonModule允许导入 JSON 模块
allowArbitraryExtensions允许任意文件扩展名
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"declaration": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
},
"types": ["vite/client"]
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"removeComments": false
},
"include": ["src"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}

可以继承其他配置文件:

{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
}
}

安装:

Terminal window
npm install -D @tsconfig/node20
npm install -D @tsconfig/recommended
npm install -D @tsconfig/strictest

使用:

{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
}
}

可用的基础配置:

  • @tsconfig/recommended - 推荐配置
  • @tsconfig/strictest - 最严格配置
  • @tsconfig/node18 - Node.js 18
  • @tsconfig/node20 - Node.js 20
  • @tsconfig/vite-react - Vite + React
  • @tsconfig/vite-react-ts - Vite + React + TS
  • @tsconfig/next - Next.js
  • @tsconfig/nuxt - Nuxt
  • @tsconfig/svelte - Svelte
  • @tsconfig/deno - Deno
  • @tsconfig/bun - Bun

一个项目可以有多个配置文件:

tsconfig.json # 主配置
tsconfig.node.json # Vite 配置文件
tsconfig.test.json # 测试配置

示例 tsconfig.node.json:

{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["vite.config.ts"]
}

然后在主配置中引用:

{
"references": [{ "path": "./tsconfig.node.json" }]
}

可以通过命令行参数覆盖配置:

Terminal window
tsc --target ES2022 --module NodeNext
tsc --noEmit --strict

确保配置了正确的 moduleResolution:

{
"compilerOptions": {
"moduleResolution": "Bundler" // 或 "NodeNext"
}
}

需要配置 pathsbaseUrl:

{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
}
}

注意: TypeScript 不会处理路径别名的转换, 需要配合构建工具(如 Vite、Webpack)或 tsc-alias

配置 typeRootstypes:

{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./src/types"],
"types": ["node", "jest"]
}
}

启用 resolveJsonModule:

{
"compilerOptions": {
"resolveJsonModule": true
}
}
import data from './data.json'
{
"compilerOptions": {
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
}
}
t1