TS tsconfig配置:掌握编译选项
详解tsconfig.json的核心配置项,优化TypeScript项目的编译行为。 · 难度:入门 · +15XP
tsconfig.json的重要性
tsconfig.json是TypeScript项目的配置文件,它定义了编译器的行为规则。一个合理的配置可以提升开发效率和代码质量。
核心配置项
{
"compilerOptions": {
"target": "ES2020", // 编译目标版本
"module": "commonjs", // 模块系统
"strict": true, // 启用所有严格检查
"outDir": "./dist", // 输出目录
"rootDir": "./src", // 源码目录
"esModuleInterop": true, // ES模块互操作性
"skipLibCheck": true // 跳过库文件检查
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}严格模式家族
| 配置项 | 作用 |
|---|---|
| strictNullChecks | 严格空值检查 |
| noImplicitAny | 禁止隐式any |
| noUnusedLocals | 报告未使用局部变量 |
| noUnusedParameters | 报告未使用参数 |
路径映射
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@utils/*": ["src/utils/*"],
"@components/*": ["src/components/*"]
}
}
}练习提示
请补全下面的tsconfig配置,使其满足:目标ES2020,严格模式开启,源码在src目录,输出到dist目录,并配置路径别名@/*映射到src/*。