@tker/tsconfig
v2.0.0
Published
共享的 TypeScript 配置包,提供多种场景的 TSConfig 预设配置。
Readme
@tker/tsconfig
共享的 TypeScript 配置包,提供多种场景的 TSConfig 预设配置。
安装
pnpm add -D @tker/tsconfig使用方式
在项目根目录创建 tsconfig.json,继承对应的配置:
Web 项目
适用于前端 Vue/React 项目:
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tker/tsconfig/web",
"include": ["src"],
"exclude": ["node_modules", "dist"]
}Node.js 项目
适用于 Node.js 后端/脚本项目:
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tker/tsconfig/node",
"include": ["src"],
"exclude": ["node_modules", "dist"]
}库项目
适用于需要生成类型声明文件的库项目:
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tker/tsconfig/library",
"include": ["src"],
"exclude": ["node_modules", "dist"]
}配置说明
base.json
基础配置,包含所有场景共用的规则:
| 选项 | 值 | 说明 |
|------|-----|------|
| target | ESNext | 编译目标 |
| module | ESNext | 模块系统 |
| moduleResolution | bundler | 模块解析策略 |
| strict | true | 严格模式 |
| noUnusedLocals | true | 检查未使用变量 |
| noUnusedParameters | true | 检查未使用参数 |
| noImplicitAny | true | 禁止隐式 any |
| verbatimModuleSyntax | true | 精确模块语法 |
| skipLibCheck | true | 跳过类型库检查 |
web.json
Web 项目配置,继承 base.json 并添加:
| 选项 | 值 | 说明 |
|------|-----|------|
| jsx | preserve | JSX 保留原样 |
| jsxImportSource | vue | Vue JSX 导入源 |
| lib | ESNext, DOM, DOM.Iterable | 包含 DOM 类型 |
| types | vite/client | Vite 客户端类型 |
| useDefineForClassFields | true | 类字段定义方式 |
node.json
Node.js 项目配置,继承 base.json 并添加:
| 选项 | 值 | 说明 |
|------|-----|------|
| lib | ESNext | 仅 ES 类型 |
| types | node | Node.js 类型 |
| moduleResolution | bundler | Bundler 解析策略 |
library.json
库项目配置,继承 base.json 并添加:
| 选项 | 值 | 说明 |
|------|-----|------|
| jsx | preserve | JSX 保留原样 |
| lib | ESNext, DOM, DOM.Iterable | 包含 DOM 类型 |
| declaration | true | 生成 .d.ts 文件 |
| noEmit | false | 允许输出文件 |
| moduleResolution | node16 | Node16 解析策略 |
自定义配置
可以覆盖继承的配置:
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tker/tsconfig/web",
"compilerOptions": {
"strictNullChecks": false,
"noUnusedLocals": false
},
"include": ["src", "types"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}严格规则说明
base.json 启用了以下严格检查:
strict- 启用所有严格选项strictNullChecks- 严格的 null 检查noImplicitAny- 禁止隐式 any 类型noImplicitOverride- 要求 override 关键字noImplicitThis- 禁止隐式 thisnoUncheckedIndexedAccess- 索引访问返回 undefinednoFallthroughCasesInSwitch- 禁止 switch 穿透
License
MIT
