Skip to content

附录C 资源与进阶学习

本附录提供TypeScript学习的优质资源、优秀开源项目和职业发展建议。


TypeScript官网

TypeScript Handbook

TypeScript API文档

TypeScript Playground

TypeScript Samples


DefinitelyTyped

ts-node

tsx

React + TypeScript

Vue 3 + TypeScript

Angular

Express + TypeScript

Fastify

NestJS

zod

pothos

trpc


Stack Overflow

TypeScript Community

dev.to TypeScript

TypeScript Deep Dive

Matt Pocock的TypeScript教程

Type Saini

TypeScript

Fireship


VS Code

WebStorm

ESLint

Prettier

TypeScript ESLint

esbuild

Vite

Webpack

npm

pnpm

yarn


版本发布重要特性
0.82012初始发布
1.02014Visual Studio支持
2.02016非空类型、控制流分析
3.02018泛型参数、Project References
4.02020可变元组类型、标记元组元素
4.52021保留装饰器提案支持
4.82022精化类型推断
5.02023装饰器标准化、const类型参数
5.12023支持注解装饰器
5.22023Using声明
5.32023延迟导入类型、更好的类型推断
5.42024NoInfer、闭包中的类型检查

独立声明

模式匹配

更好的一样类型检查


初级(0-2年)

  • TypeScript基础类型和语法
  • 理解编译流程和类型检查
  • 使用主流框架(React/Vue)
  • 基本调试能力

中级(2-5年)

  • 高级类型编程
  • 类型守卫和泛型
  • 运行时验证
  • 框架内部原理
  • 性能优化

高级(5年+)

  • 复杂类型系统设计
  • 装饰器和元编程
  • DSL设计
  • 框架贡献
  • 架构决策

常见问题

  1. TypeScript与JavaScript的区别
  2. any vs unknown的区别
  3. interface vs type的区别
  4. 如何实现泛型约束
  5. 什么是类型守卫
  6. 条件类型的应用
  7. never类型的用途
  8. 装饰器的原理
  9. 如何进行运行时验证
  10. strict模式的配置

编码题目

  1. 实现一个工具类型(如Partial、Pick、Omit)
  2. 实现泛型版的栈或队列
  3. 实现类型安全的API客户端
  4. 实现自定义的类型守卫
  5. 实现DeepPartial等递归类型
级别美国(USD/年)中国(人民币/年)
初级$70,000-100,00020-35万
中级$100,000-150,00035-60万
高级$150,000-250,00060-100万
架构师/专家$250,000+100万+

  1. 夯实基础

    • 掌握基础类型和类型注解
    • 理解类型推断机制
    • 熟悉常用语法
  2. 深入类型系统

    • 学习泛型和约束
    • 掌握条件类型
    • 理解映射类型
  3. 项目实战

    • 在实际项目中使用TypeScript
    • 配置tsconfig.json
    • 处理外部库类型
  4. 持续进阶

    • 学习装饰器和元编程
    • 探索类型级编程
    • 贡献开源项目

代码组织

  • 模块化:按功能模块组织代码
  • 类型分离:接口定义与实现分离
  • 一致性:团队内统一类型命名规则

命名规范

  • 类型:PascalCase(如User、ApiResponse)
  • 接口前缀:I开头(可选,如IUser)
  • 枚举成员:PascalCase(如HttpStatus.Ok)
  • 工具类型:PascalCase(如DeepPartial)

代码示例

// 推荐
interface User {
id: string;
name: string;
email: string;
createdAt: Date;
}
type UserList = User[];
// 不推荐
interface IUser {
strId: string;
strName: string;
}

错误处理

// 使用Result类型
type Result<T, E = Error> =
| { success: true; data: T }
| { success: false; error: E };
// 或使用异常
async function fetchUser(id: string): Promise<User> {
try {
const response = await fetch(`/users/${id}`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.json();
} catch (error) {
console.error("Failed to fetch user:", error);
throw error;
}
}

类型验证

import { z } from "zod";
const UserSchema = z.object({
name: z.string().min(1).max(100),
email: z.string().email(),
age: z.number().int().positive().optional()
});
function createUser(data: unknown): User {
const result = UserSchema.safeParse(data);
if (!result.success) {
throw new Error(`Validation failed: ${result.error.message}`);
}
return result.data;
}

  • React/Vue + TypeScript
  • Node.js后端 + TypeScript
  • 数据库类型(Prisma、Drizzle)
  • 构建工具(Vite、Webpack)
  • 代码分割与懒加载
  • 微前端架构
  • 形式语言类型系统
  • 依赖注入框架
  • ORM类型安全
  • 为DefinitelyTyped贡献类型
  • 参与TypeScript本身
  • 维护开源库

Terminal window
# 初始化项目
npm init -y
npm install -D typescript
# 创建配置
npx tsc --init
# 编译
npx tsc
npx tsc --watch
# 类型检查(不编译)
npx tsc --noEmit
# 快速运行
npx ts-node src/index.ts
npx tsx src/index.ts
// tsconfig.json 推荐配置
{
"compilerOptions": {
"target": "ES2020",
"module": "Node16",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node16",
"resolveJsonModule": true,
"outDir": "./dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true
}
}
// 类型定义
import type { User, Product } from "./types";
// 运行时
import express from "express";
import { z } from "zod";
// 工具
import { useState, useEffect } from "react";
import { map, filter, reduce } from "lodash-es";

本书系统地介绍了从Python到TypeScript的转型路径。TypeScript作为JavaScript的超集,不仅保留了动态语言的灵活性,还添加了强大的静态类型系统,让大规模前端开发更加可靠。

希望你在学习完本书后,能够:

  • 理解TypeScript类型系统的核心概念
  • 掌握从Python到TypeScript的思维转换
  • 在实际项目中灵活运用TypeScript
  • 享受类型安全带来的开发体验提升

继续探索,不断进步!


推荐继续阅读

GitHub仓库

如果你发现书中有任何问题或有任何改进建议,欢迎在以下仓库提交Issue或PR: