tsconfig.json配置解析
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".next/dev/types/**/*.ts",
"**/*.mts"
],
"exclude": ["node_modules"]
}
[我严格遵守规则]
这是 TypeScript 编译器的配置文件,逐项解释:
compilerOptions 编译选项
| 配置 | 值 | 含义 |
|---|---|---|
target |
ES2017 |
编译输出的 JS 版本,支持 async/await 原生语法 |
lib |
dom, dom.iterable, esnext |
引入的内置类型库:DOM API、可迭代 DOM、最新 ES 特性 |
allowJs |
true |
允许项目中混用 .js 文件 |
skipLibCheck |
true |
跳过 node_modules 里 .d.ts 文件的类型检查(加速编译) |
strict |
true |
开启所有严格类型检查(strictNullChecks、noImplicitAny 等) |
noEmit |
true |
只做类型检查,不输出编译产物(由 Next.js/Turbopack 负责实际编译) |
esModuleInterop |
true |
允许 import X from 'xxx' 方式导入 CommonJS 模块 |
module |
esnext |
模块格式用 ES Modules |
moduleResolution |
bundler |
模块解析策略用打包器模式(Next.js 专用,支持省略扩展名等) |
resolveJsonModule |
true |
允许直接 import .json 文件 |
isolatedModules |
true |
每个文件独立编译(兼容 SWC/Babel 的要求) |
jsx |
react-jsx |
使用 React 17+ 新 JSX transform,不需要每个文件都 import React |
incremental |
true |
增量编译,缓存上次结果,加快重复构建速度 |
plugins → next |
— | 启用 Next.js 专属 TS 插件(提供页面类型、路由类型推断等) |
paths → @/* |
./src/* |
路径别名,@/foo 等价于 src/foo |
include — 纳入编译的文件
next-env.d.ts ← Next.js 自动生成的环境类型声明
**/*.ts / **/*.tsx ← 所有 TS/TSX 源文件
.next/types/**/*.ts ← Next.js 生成的路由类型(生产)
.next/dev/types/**/*.ts← Next.js 生成的路由类型(开发)
**/*.mts ← ES Module 专用 TS 文件
exclude — 排除编译的文件
node_modules ← 第三方包不参与类型检查(skipLibCheck 已覆盖,双重保险)
