🎯 TypeScript 的价值
TypeScript 是 JavaScript 的超集,为其添加了静态类型系统。根据 2023 年 State of JS 调查,TypeScript 的采用率已达到 93%,成为前端开发的标准配置。
类型安全
编译时发现错误,减少运行时错误 68%
开发效率
IDE 智能提示,代码补全效率提升 45%
重构信心
类型系统保证重构安全性,大型项目必备
可维护性
类型即文档,提升代码可读性和可维护性
TypeScript 采用率趋势
201956%
202068%
202178%
202287%
202393%
💡 独特观点
TypeScript 的价值不在于"类型检查"本身,而在于类型驱动开发(Type-Driven Development)。通过设计精准的类型,你可以在编译时捕获业务逻辑错误,而不仅仅是语法错误。类型系统是业务规则的守护者。
📝 基础类型系统
TypeScript 的基础类型系统是构建复杂类型的基石。让我们从底层理解它们。
1. 原始类型
📝 原始类型注解
// 原始类型
let name: string = "Alice"
let age: number = 30
let isActive: boolean = true
let nullable: null = null
let undefinedValue: undefined = undefined
// 特殊类型
let anything: any = "可以是任何类型" // ❌ 避免使用
let unknownValue: unknown = "安全版本的 any" // ✅ 推荐
let neverValue: never // 永远不会发生的值
let voidValue: void = undefined // 函数没有返回值2. 数组与元组
📝 数组与元组
// 数组
let numbers: number[] = [1, 2, 3]
let strings: Array<string> = ["a", "b"] // 泛型语法
// 元组(固定长度和类型的数组)
let person: [string, number] = ["Alice", 30]
let response: [number, string] = [200, "OK"]
// 可选元素
let optionalTuple: [string, number?] = ["Alice"] // ✅
// 剩余元素
let restTuple: [string, ...number[]] = ["Alice", 1, 2, 3]3. 枚举
📝 枚举类型
// 数字枚举(默认从 0 开始)
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
// 字符串枚举(更推荐)
enum Status {
Active = "ACTIVE",
Inactive = "INACTIVE",
Pending = "PENDING"
}
// 常量枚举(编译时内联,减少代码体积)
const enum Size {
Small = 1,
Medium = 2,
Large = 3
}📊 枚举 vs 常量对象
| 特性 | 枚举 (enum) | 常量对象 (const object) |
|---|---|---|
| 类型安全 | ✅ 是 | ✅ 是 |
| 反向映射 | ✅ 支持 | ❌ 不支持 |
| tree-shaking | ❌ 差 | ✅ 好 |
| 编译后体积 | 较大 | 较小(内联) |
| 推荐度 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
✅ 最佳实践
- 优先使用
const enum或const object替代普通枚举 - 避免使用
any,使用unknown替代 - 使用
as const断言创建只读常量
🚀 高级类型特性
TypeScript 的高级类型特性让我们能够构建精确、可复用、类型安全的类型系统。
1. 联合类型与交叉类型
📝 联合类型(Union Types)
// 联合类型:可以是多种类型之一
let id: string | number
id = "abc" // ✅
id = 123 // ✅
// 字面量联合类型
type Status = "pending" | "approved" | "rejected"
let status: Status = "pending"
// 可辨别联合(Discriminated Unions)
type Shape =
| { kind: "circle"; radius: number }
| { kind: "rectangle"; width: number; height: number }
| { kind: "triangle"; base: number; height: number }
function getArea(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2
case "rectangle":
return shape.width * shape.height
case "triangle":
return 0.5 * shape.base * shape.height
}
}📝 交叉类型(Intersection Types)
// 交叉类型:合并多个类型
interface User {
name: string
email: string
}
interface Admin {
permissions: string[]
}
type AdminUser = User & Admin
// AdminUser 必须同时满足 User 和 Admin
const admin: AdminUser = {
name: "Alice",
email: "[email protected]",
permissions: ["read", "write", "delete"]
}
// 合并多个对象类型
type Combine<T, U> = T & U2. 类型守卫
📝 类型守卫(Type Guards)
// typeof 类型守卫
function processValue(value: string | number) {
if (typeof value === "string") {
// 此处 value 被收窄为 string
return value.toUpperCase()
} else {
// 此处 value 被收窄为 number
return value.toFixed(2)
}
}
// instanceof 类型守卫
class Bird {
fly() { console.log("Flying...") }
}
class Fish {
swim() { console.log("Swimming...") }
}
function move(animal: Bird | Fish) {
if (animal instanceof Bird) {
animal.fly()
} else {
animal.swim()
}
}
// 自定义类型守卫
function isString(value: unknown): value is string {
return typeof value === "string"
}
if (isString(someValue)) {
// someValue 被收窄为 string
console.log(someValue.toUpperCase())
}💡 深入理解
类型守卫的本质是类型收窄(Type Narrowing)。TypeScript 编译器在 if 分支中能够确定变量的具体类型,这是 TypeScript 类型系统的核心能力之一。
📝 总结
TypeScript 类型系统是现代前端开发的基石。通过本文的学习,你应该掌握了:
🎯 核心要点
- 基础类型:原始类型、数组、元组、枚举的使用场景和最佳实践
- 高级类型:联合类型、交叉类型、类型守卫的深入理解
- 泛型编程:创建可复用、类型安全的组件和函数
- 条件类型:根据类型关系动态选择类型
- 映射类型:基于现有类型创建新类型
- 工具类型:TypeScript 内置工具类型的实战应用