Skip to content

TypeScript 基础配置标准

最小可运行配置

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "outDir": "./dist",
    "rootDir": "./src",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

配置选项详解

语言和环境

选项推荐值说明
targetES2022ECMAScript 目标版本
lib["ES2022"]可用的标准库
moduleNodeNext模块系统(Node.js 16+)
moduleResolutionNodeNext模块解析策略

输出控制

选项推荐值说明
outDir./dist编译输出目录
rootDir./src源码根目录
sourceMaptrue生成 source map
declarationtrue生成类型声明
declarationMaptrue生成声明 map
removeCommentsfalse保留注释(文档需要)

模块互操作

选项推荐值说明
esModuleInteroptrueES 模块互操作
allowSyntheticDefaultImportstrue合成默认导入
resolveJsonModuletrue导入 JSON 文件
isolatedModulestrue单文件转译兼容

标准配置模板

开发环境(tsconfig.json)

json
{
  "compilerOptions": {
    /* 语言和环境 */
    "target": "ES2022",
    "lib": ["ES2022"],
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    
    /* 输出 */
    "outDir": "./dist",
    "rootDir": "./src",
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,
    "removeComments": false,
    
    /* 互操作 */
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    
    /* 类型检查 */
    "strict": false,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    
    /* 完整性 */
    "noEmitOnError": true,
    "preserveConstEnums": true,
    "preserveWatchOutput": true,
    
    /* 高级 */
    "incremental": true,
    "tsBuildInfoFile": ".tsbuildinfo"
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.json"
  ],
  "exclude": [
    "node_modules",
    "dist",
    "**/*.test.ts",
    "**/*.spec.ts"
  ]
}

生产构建(tsconfig.build.json)

json
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "sourceMap": false,
    "declaration": true,
    "removeComments": true,
    "noEmitOnError": true
  },
  "exclude": [
    "node_modules",
    "dist",
    "**/*.test.ts",
    "**/*.spec.ts",
    "**/__tests__/**",
    "**/__mocks__/**"
  ]
}

Node.js 版本对应

Node.jsTargetLib说明
18.xES2022ES2022LTS,推荐
20.xES2022ES2023Current LTS
22.xES2023ES2023Latest

项目类型配置

库项目

json
{
  "compilerOptions": {
    "target": "ES2020",  // 更广泛的兼容性
    "module": "ESNext",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "composite": true
  }
}

应用项目

json
{
  "compilerOptions": {
    "target": "ES2022",  // 使用最新特性
    "module": "NodeNext",
    "declaration": false,  // 应用不需要声明
    "sourceMap": true
  }
}

Monorepo 项目

json
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "declarationMap": true,
    "incremental": true
  },
  "references": [
    { "path": "./packages/core" },
    { "path": "./packages/utils" }
  ]
}

文件组织最佳实践

project/
├── tsconfig.json           # 基础配置
├── tsconfig.build.json     # 构建配置
├── tsconfig.test.json      # 测试配置
├── src/
│   ├── index.ts
│   └── types/
│       └── global.d.ts
├── dist/                   # 编译输出
└── types/                  # 类型声明输出

配置继承策略

json
// tsconfig.base.json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "strict": true
  }
}

// tsconfig.json
{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./dist"
  },
  "include": ["src/**/*"]
}

性能优化配置

json
{
  "compilerOptions": {
    "incremental": true,              // 增量编译
    "tsBuildInfoFile": ".tsbuildinfo", // 构建信息缓存
    "assumeChangesOnlyAffectDirectDependencies": true,
    "skipLibCheck": true,             // 跳过 .d.ts 检查
    "skipDefaultLibCheck": true       // 跳过默认库检查
  }
}

常见问题配置

解决路径问题

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

解决 Node.js 类型

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

记住:从最小配置开始,根据需要逐步添加选项。

基于 MIT 许可发布