tsconfig.json 必开配置:2026 版速查
· 3 min read
tsconfig.json 选项有 100+ 个,但日常 80% 场景只需要 10 个左右。
strict: true:8 个严格检查全开,新项目必开target: "ES2022":覆盖 Node 18+ 和现代浏览器module: "NodeNext":Node ESM 现代写法skipLibCheck: true:跳过.d.ts检查,构建快 30%+noUncheckedIndexedAccess: true:arr[0] 类型变 T | undefined
必开配置(2026 年推荐)
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noFallthroughCasesInSwitch": true,
"exactOptionalPropertyTypes": true
}
}
每一项的理由:
target: ES2022——class fields、top-level await、Error Cause都覆盖module: NodeNext—— Node 项目的现代选择strict: true—— 一次性打开 8 个严格检查,等价于:noImplicitAnystrictNullChecksstrictFunctionTypesstrictBindCallApplystrictPropertyInitializationnoImplicitThisuseUnknownInCatchVariablesalwaysStrict
esModuleInterop—— 让import React from 'react'正常工作skipLibCheck—— 跳过@types/*包的类型检查,构建快很多noUncheckedIndexedAccess——arr[0]类型变成T | undefined,强制处理边界verbatimModuleSyntax—— import 时必须明确写import type
已过时的选项
warning
以下选项在新项目里不要再用:
module: "CommonJS"—— 现代 Node ESM 是默认,写"NodeNext"让 TS 自动判断moduleResolution: "Node"——"NodeNext"或"Bundler"是更新版target: "ES5"—— Node 18+、现代浏览器都支持 ES2022experimentalDecorators—— TC39 标准装饰器已稳定(TS 5.0+),用新写法importHelpers—— tslib 已经过时,TS 5.0+ 自动处理