TypeScript 预设
安装
bash
pnpm add -D @uniboot/tsconfig typescript@uniboot/tsconfig 仅包含若干 *.json 预设;typescript 本体由项目安装。web.json / web-app.json 通过 compilerOptions.types 包含 vite/client(Vite 自带的客户端类型),用于使 import.meta.env 等可被 TS 识别。
各 JSON 适用场景
| 子路径 | display | 建议用途 |
|---|---|---|
base.json | Base | 严格 TS 基线,无 DOM;可被其它 extends |
web.json | Web Package | 含 DOM / DOM.Iterable、moduleResolution: 'bundler'、types: ['vite/client'],偏 库 / 包 与 Vite 客户端共用 |
web-app.json | Web Application | 在 web.json 上明确再次声明 types: ['vite/client'],适合 SPA 应用源码 |
vue-app.json | Vue / Vite 应用 | 在 web-app.json 之上空壳继承,名称体现 Vue 应用,通常给 Vue 应用 tsconfig.app.json |
node.json | Node Config | 工具脚本、Node 侧配置(如 vite.config.ts 的 Node 解析);types: ['node']、moduleResolution: 'bundler' |
library.json | Web Application | 在 base.json 上为发布库打开 declaration: true、noEmit: false,并加入 DOM 与 useDefineForClassFields |
上表
display为 JSON 中的实际字段;web-app.json与library.json的display同为Web Application,但用途不同——以extends链与compilerOptions为准。
引用方式
json
{
"extends": "@uniboot/tsconfig/base.json",
"include": ["packages"]
}应用(Vue + Vite)常见拆法:
tsconfig.app.json→extends: "@uniboot/tsconfig/vue-app.json"tsconfig.node.json(Node 端编vite.config.ts)→extends: "@uniboot/tsconfig/node.json"- 根
tsconfig.json通常仅做references集合,不直接extends。
发布库 / 组件包:
tsconfig.json→extends: "@uniboot/tsconfig/library.json"- 业务源码可再写一份
tsconfig.app.json(extends: ".../web.json")配 IDE / 类型检查。
import.meta.env 与 Vite 类型
web.json / web-app.json 已包含 "vite/client",即可使用 Vite 提供的 import.meta.env 等类型。自定义 VITE_* 等请在应用内增加 env.d.ts:
ts
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_APP_TITLE: string
}严格性与基线
base.json 默认开启的关键开关(节选):
strict: true、strictNullChecks: truenoImplicitAny/noImplicitOverride/noImplicitThisnoUncheckedIndexedAccess、noUnusedLocals、noUnusedParameters、noFallthroughCasesInSwitchmodule: 'ESNext'、target: 'ESNext'、moduleDetection: 'force'isolatedModules: true、verbatimModuleSyntax: true、esModuleInterop: trueexperimentalDecorators: true、skipLibCheck: trueexclude:**/node_modules/**、**/dist/**、**/.turbo/**
放宽 / 调整时请尽量在应用侧的 tsconfig 覆盖,避免改动预设;如确有团队级共识,请回到 uniboot-configs/packages/tsconfig 提 PR。