CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
TypeScript 进阶/课时/工具链
RawBlame
课时目录12 节

TypeScript 进阶

  1. 01TS 进阶地图
  2. 02原始类型深入
  3. 03集合类型
  4. 04断言与收窄
  5. 05内置工具类型
  6. 06高级类型
  7. 07类与 OOP
  8. 08函数类型进阶
  9. 09装饰器
  10. 10命名空间
  11. 11工具链
  12. 12综合练习

工具链

tsc、ts-node、compiler options、lint/format、build tools。

30 分钟

工具链

写出正确类型只是第一步;tsc、ts-node、compiler options、ESLint、Prettier、打包器 共同决定开发体验与产物质量。本课梳理 TypeScript 工具链,衔接 typescript-essentials,并对照 platform 与 nextjs-essentials(SWC 编译)、nestjs-essentials(tsc + nest build)的实际配置。

学习目标

  • 读懂 tsconfig 核心选项:strict、moduleResolution、paths
  • 区分 tsc 类型检查 vs 打包器转译
  • 配置 ESLint TypeScript 规则与项目引用

tsconfig 核心选项

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "jsx": "preserve",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}
选项作用
strict开启全部严格检查
noUncheckedIndexedAccess索引访问返回 T | undefined
moduleResolution: bundler配合 Vite/Next 解析
paths路径别名,需打包器同步配置

tsc 职责边界

tsc --noEmit          → 仅类型检查(CI 常用)
tsc                   → 类型检查 + 输出 .js + .d.ts
Next.js / Vite        → SWC/esbuild 转译,tsc 或 IDE 做类型
# 仅检查
npx tsc --noEmit

# 生成声明文件
npx tsc --declaration --emitDeclarationOnly

ts-node 与 tsx

# 开发脚本直接运行 TS
npx tsx scripts/seed.ts

# ts-node(需 tsconfig 配合)
npx ts-node --esm scripts/migrate.ts

ESLint + Prettier

// .eslintrc 片段
{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended-type-checked"
  ],
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "rules": {
    "@typescript-eslint/no-explicit-any": "warn",
    "@typescript-eslint/no-floating-promises": "error"
  }
}

Project References

// packages/shared/tsconfig.json
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist"
  }
}

// apps/platform/tsconfig.json
{
  "references": [{ "path": "../../packages/shared" }]
}

练习

  • 在 platform 运行 tsc --noEmit 并修复一条 strict 报错
  • 添加 noUncheckedIndexedAccess 观察对现有代码的影响
  • 对照 Next.js 与 NestJS 项目的 tsconfig 差异

小结

工具链 = tsconfig 严格度 + 类型检查与转译分工 + Lint 守门。 下一课综合练习。

加载本节练习…
← 上一课命名空间下一课 →综合练习