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.jsonBase严格 TS 基线,无 DOM;可被其它 extends
web.jsonWeb PackageDOM / DOM.IterablemoduleResolution: 'bundler'types: ['vite/client'],偏 库 / 包 与 Vite 客户端共用
web-app.jsonWeb Applicationweb.json 上明确再次声明 types: ['vite/client'],适合 SPA 应用源码
vue-app.jsonVue / Vite 应用web-app.json 之上空壳继承,名称体现 Vue 应用,通常给 Vue 应用 tsconfig.app.json
node.jsonNode Config工具脚本、Node 侧配置(如 vite.config.ts 的 Node 解析);types: ['node']moduleResolution: 'bundler'
library.jsonWeb Applicationbase.json 上为发布库打开 declaration: truenoEmit: false,并加入 DOMuseDefineForClassFields

上表 display 为 JSON 中的实际字段;web-app.jsonlibrary.jsondisplay 同为 Web Application,但用途不同——以 extends 链与 compilerOptions 为准。

引用方式

json
{
  "extends": "@uniboot/tsconfig/base.json",
  "include": ["packages"]
}

应用(Vue + Vite)常见拆法:

  • tsconfig.app.jsonextends: "@uniboot/tsconfig/vue-app.json"
  • tsconfig.node.json(Node 端编 vite.config.ts)→ extends: "@uniboot/tsconfig/node.json"
  • tsconfig.json 通常仅做 references 集合,不直接 extends

发布库 / 组件包:

  • tsconfig.jsonextends: "@uniboot/tsconfig/library.json"
  • 业务源码可再写一份 tsconfig.app.jsonextends: ".../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: truestrictNullChecks: true
  • noImplicitAny / noImplicitOverride / noImplicitThis
  • noUncheckedIndexedAccessnoUnusedLocalsnoUnusedParametersnoFallthroughCasesInSwitch
  • module: 'ESNext'target: 'ESNext'moduleDetection: 'force'
  • isolatedModules: trueverbatimModuleSyntax: trueesModuleInterop: true
  • experimentalDecorators: trueskipLibCheck: true
  • exclude: **/node_modules/****/dist/****/.turbo/**

放宽 / 调整时请尽量在应用侧tsconfig 覆盖,避免改动预设;如确有团队级共识,请回到 uniboot-configs/packages/tsconfig 提 PR。