tsconfig 详解与严格模式
约 213 字小于 1 分钟
布欧-Lewyon
2026-05-16
首页 › TypeScript › 工程化 › tsconfig 详解与严格模式
tsconfig.json 核心字段
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"rootDir": "./src",
"declaration": true,
"sourceMap": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}strict 系列详解
| 选项 | 作用 |
|---|---|
strict: true | 一键开启下面所有 |
strictNullChecks | null/undefined 不可赋值给其他类型 |
strictFunctionTypes | 函数参数逆变检查 |
strictBindCallApply | bind/call/apply 参数检查 |
strictPropertyInitialization | 类属性必须在构造函数初始化 |
noImplicitAny | 禁止隐式 any |
noImplicitReturns | 函数所有路径需有返回值 |
noUncheckedIndexedAccess | 索引访问结果包含 undefined |
moduleResolution
| 策略 | 适用场景 |
|---|---|
classic | 旧版(不推荐) |
node | Node.js < 12 |
node16 | Node.js 16+ |
nodenext | Node.js 最新 |
bundler | Vite / Webpack / esbuild(推荐) |
小结
| 配置 | 推荐值 |
|---|---|
strict | true |
moduleResolution | "bundler"(现代构建工具) |
target | "ES2022" |
declaration | true(库项目) |
