tsconfig.json 配置

tsconfig.json 是 TypeScript 项目的配置文件,定义编译选项、文件包含规则等。理解 tsconfig.json 的配置对于构建 TypeScript 项目至关重要。

基本结构

tsconfig.json 的基本结构包含编译选项和文件配置。

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

compilerOptions

compilerOptions 定义编译器的行为。

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "lib": ["ES2020", "DOM"],
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

include 和 exclude

include 指定包含的文件,exclude 指定排除的文件。

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

include 支持通配符:* 匹配任意文件,**/ 匹配任意目录。

files

files 明确指定包含的文件。

{
  "files": ["src/index.ts", "src/main.ts"]
}

files 优先级高于 include

extends

extends 继承其他配置文件。

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

可以继承本地文件或 npm 包的配置。

target

target 指定编译后的 JavaScript 版本。

{
  "compilerOptions": {
    "target": "ES2020"
  }
}

可选值:ES3ES5ES6ES2017ES2020ESNext

module

module 指定模块系统。

{
  "compilerOptions": {
    "module": "ESNext"
  }
}

可选值:CommonJSAMDSystemUMDES6ESNext

lib

lib 指定运行时库。

{
  "compilerOptions": {
    "lib": ["ES2020", "DOM", "DOM.Iterable"]
  }
}

lib 决定可用的 API 类型定义。

outDir 和 rootDir

outDir 指定输出目录,rootDir 指定源码目录。

{
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src"
  }
}

strict

strict 启用所有严格类型检查选项。

{
  "compilerOptions": {
    "strict": true
  }
}

等同于启用以下选项:

  • noImplicitAny
  • strictNullChecks
  • strictFunctionTypes
  • strictBindCallApply
  • strictPropertyInitialization
  • noImplicitThis
  • alwaysStrict

路径映射

路径映射可以简化模块导入。

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

使用别名导入模块。

import { Button } from "@components/Button"
import { formatDate } from "@utils/format"

实际应用

一个完整的 tsconfig.json 配置示例。

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "moduleResolution": "node",
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "typeRoots": ["./types", "./node_modules/@types"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "**/*.test.ts"]
}

小结

tsconfig.json 是 TypeScript 项目的核心配置文件。compilerOptions 定义编译器行为,includeexclude 控制文件包含。target 指定 JavaScript 版本,module 指定模块系统。strict 启用严格类型检查。路径映射可以简化模块导入。合理配置 tsconfig.json 可以提高开发效率和代码质量。