tsconfig.json 配置清单

发布于:2025-03-05 ⋅ 阅读:(145) ⋅ 点赞:(0)

tsconfig.json 配置清单

基础结构

{
  "compilerOptions": {
    // 编译选项
  },
  "include": [
    // 指定需要编译的文件或目录
  ],
  "exclude": [
    // 指定不需要编译的文件或目录
  ],
  "extends": "./base-tsconfig.json",
  "files": [
    // 需要编译的具体文件
  ],
  "references": [
    // 用于 TypeScript 项目引用
  ]
}

compilerOptions 详细配置

按类别整理,每个配置项都附带解释和可能的值。


1. ECMAScript 版本、模块系统

配置项 说明 可能的值
target 目标 ECMAScript 版本 ES3, ES5, ES6, ES2015, ES2016, ES2017, ES2018, ES2019, ES2020, ES2021, ES2022, ESNext
module 目标模块系统 CommonJS, AMD, UMD, System, ES6, ES2015, ES2020, ESNext
lib 需要包含的库 ["ES6", "DOM", "ESNext", "ES2020", "ES2019"]
moduleResolution 模块解析策略 Classic, Node, Bundler
moduleDetection 何时启用模块支持 auto, legacy, force
rootDirs 指定多个根目录 ["src", "shared"]
allowJs 允许编译 .js 文件 true, false
checkJs 启用 JS 文件类型检查 true, false
resolveJsonModule 允许导入 JSON 文件 true, false
esModuleInterop 兼容 ES 模块默认导入 true, false
preserveSymlinks 不解析软链接 true, false

2. 输出控制

配置项 说明 可能的值
outDir 指定输出目录 "./dist"
outFile 输出为单个 .js 文件 "./bundle.js"
rootDir 指定源代码根目录 "./src"
declaration 生成 .d.ts 声明文件 true, false
declarationMap 生成 .d.ts.map 映射文件 true, false
declarationDir .d.ts 输出目录 "./types"
sourceMap 生成 .map 文件 true, false
inlineSourceMap 内联 sourceMap true, false
removeComments 移除注释 true, false
emitBOM 输出带有 BOM 头的文件 true, false

3. 严格模式

配置项 说明 可能的值
strict 开启所有严格模式 true, false
noImplicitAny 禁止隐式 any 类型 true, false
strictNullChecks 启用严格的空值检查 true, false
strictFunctionTypes 启用函数参数严格检查 true, false
strictBindCallApply 启用 bindcallapply 检查 true, false
strictPropertyInitialization 要求类属性初始化 true, false
useUnknownInCatchVariables catch 语句变量默认 unknown true, false

4. 代码检查

配置项 说明 可能的值
noUnusedLocals 禁止未使用的局部变量 true, false
noUnusedParameters 禁止未使用的函数参数 true, false
noImplicitReturns 要求函数必须有返回值 true, false
noFallthroughCasesInSwitch 禁止 switch 语句 fallthrough true, false
allowUnreachableCode 允许无法访问的代码 true, false

5. ES 特性支持

配置项 说明 可能的值
experimentalDecorators 启用实验性的装饰器 true, false
emitDecoratorMetadata 生成装饰器的元数据 true, false
downlevelIteration 向低版本 ES 兼容迭代器 true, false

6. 其他

配置项 说明
baseUrl 模块解析的基准路径
paths 配置模块别名
types 指定要包含的类型定义
allowSyntheticDefaultImports 允许默认导入非 ES6 模块
forceConsistentCasingInFileNames 强制文件名大小写一致

7. includeexcludefiles

{
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"],
  "files": ["index.ts", "types.d.ts"]
}

8. extends

{
  "extends": "./base-tsconfig.json"
}

9. references

{
  "references": [
    { "path": "./packages/core" },
    { "path": "./packages/utils" }
  ]
}

10. 示例

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "strict": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "sourceMap": true,
    "resolveJsonModule": true,
    "allowJs": true,
    "incremental": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}


网站公告

今日签到

点亮在社区的每一天
去签到