- tsconfig.json配置
- compilerOptions 配置编译器行为
- include 匹配编译文件
- exclude 排除不需要的编译文件
- extends 继承别的tsconfg.json文件
- files 是一个字符串数组,用于指定显式包含在编译中的文件的路径。与 include 不同,它包括那些无法通过匹配模式进行自动推断的文件
- 注意 使用
files 选项可以精确控制要编译的文件,适用于只想编译项目的特定部分或仅包含必要文件的情况。注意,如果使用了 files 选项,则编译器将忽略 include 和 exclude 选项
{
"compilerOptions": {
"target": "es6"
},
"files": [
"src/app.ts",
"src/utils.ts"
]
}
- compilerOptions
compilerOptions 编译器行为分为以下几个配置
- 工程化(Project)
- 语言与环境(Language and Environment)
- JavaScript支持(JavaScript Support)
- 代码输出(Emit)
- Interop Constraints
- 类型检测(Type Checking)
- 完整性检测(Completeness)
通常打包Typescript compilerOptions选项
- 编译目标和模块系统配置:
target:指定编译后的 JavaScript 目标版本。
module:指定生成的 JavaScript 模块系统。
- 模块解析配置:
moduleResolution:指定模块解析策略。
baseUrl:指定用于解析非相对模块名称的基本目录。
paths:指定模块名称的映射到实际文件或目录的路径。
- 代码输出配置:
outDir:指定编译输出的目录。
outFile:将所有输出文件合并为一个文件。
declaration:是否生成声明文件(.d.ts)。
- 类型检查和严格性配置:
strict:启用所有的严格类型检查选项。
noImplicitAny:禁止隐式的 any 类型。
strictNullChecks:启用严格的 null 检查。
- 路径和映射配置:
paths:指定模块名称的映射到实际文件或目录的路径。
rootDirs:指定多个根源文件目录。
- 其他常用配置:
allowJs:是否允许编译器编译 JavaScript 文件。
esModuleInterop:允许以常规方式导入非 TypeScript 模块。
element-plus tsconfig.json配置
{
"compilerOptions": {
"composite": true,
"lib": ["ES2018", "DOM", "DOM.Iterable"],
"types": ["unplugin-vue-macros/macros-global"],
"target": "es2018",
"module": "esnext",
"baseUrl": ".",
"paths": {
"@element-plus/*": ["packages/*"]
},
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": false,
"strict": true,
"noUnusedLocals": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"removeComments": false,
"rootDir": ".",
"skipLibCheck": true,
"allowJs": false,
"jsx": "preserve",
"esModuleInterop": true
},
"include": ["packages", "typings/components.d.ts", "typings/env.d.ts"],
"exclude": [
"node_modules",
"**/dist",
"**/__tests__/**/*",
"**/gulpfile.ts",
"**/test-helper",
"packages/test-utils",
"**/*.md"
]
}