TS tsconfig 配置:项目类型设置详解
掌握 tsconfig.json 的核心配置选项,优化 TypeScript 编译行为。 · 难度:入门 · +15XP
tsconfig.json 的重要性
tsconfig.json 是 TypeScript 项目的配置文件,定义了编译选项、文件包含规则和项目引用。合理的配置可以提升开发效率、保证代码质量并优化输出结果。
核心配置项
| 选项 | 说明 | 推荐值 |
|---|---|---|
| target | 编译目标 ECMAScript 版本 | ES2020 或更高 |
| module | 模块系统 | ESNext / CommonJS |
| strict | 启用所有严格类型检查 | true |
| outDir | 输出目录 | ./dist |
| rootDir | 源文件目录 | ./src |
| include | 包含的文件模式 | ['src/**/*'] |
代码示例
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"strict": true,
"outDir": "./dist",
"rootDir": "./src",
"esModuleInterop": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}练习提示:根据要求配置 tsconfig,启用严格模式并设置正确的路径。