TypeScript 是由 Microsoft 开发和维护的开源编程语言,是 JavaScript 的超集(Superset)。它在 JS 的基础上添加了可选的静态类型系统和基于类的面向对象编程,代码最终编译为纯 JavaScript 运行。TypeScript 已成为现代前端、Node.js 和全栈开发的事实标准——React、Vue、Angular、Next.js、Nest.js 等主流框架的默认语言。
一、TypeScript 解决了什么问题
1.1 JavaScript 的类型痛点
// JS:运行时才发现类型错误
function add(a, b) {
return a + b;
}
add(1, '2'); // "12" — 行为不符合预期,但代码"正常运行"
add(null, undefined); // NaN — 悄无声息地失败
// TS:编译时就发现错误
function add(a: number, b: number): number {
return a + b;
}
add(1, '2'); // ❌ 编译错误:Argument of type 'string' is not assignable to parameter of type 'number'
1.2 TS 的核心价值
| 价值 | 说明 |
|---|---|
| 编译时错误检查 | 在代码运行前就捕获类型错误 |
| 智能提示/自动补全 | IDE(VS Code)基于类型提供精准的代码提示 |
| 重构 confidently | 重命名、提取函数时,TS 确保引用全部被更新 |
| 自文档化 | 类型签名本身就是接口文档 |
| 团队协作 | 类型约束减少沟通成本,降低误用概率 |
二、核心类型系统
2.1 基础类型
let name: string = 'Alice';
let age: number = 25;
let isActive: boolean = true;
let data: any = 'anything'; // 尽量避免使用 any
let unknownData: unknown = 'safe any'; // 比 any 安全,使用前需类型收窄
let nothing: void; // 函数无返回值
let neverValue: never; // 永不会发生的类型(如抛出错误的函数)
let nullable: string | null = null; // 联合类型
2.2 接口(Interface)与类型别名(Type)
// Interface:可扩展,适合对象形状定义
interface User {
id: number;
name: string;
email?: string; // 可选属性
readonly createdAt: Date; // 只读
}
interface Admin extends User {
permissions: string[];
}
// Type:更灵活,支持联合类型、交叉类型
type ID = string | number;
type AdminUser = User & { permissions: string[] };
type Status = 'pending' | 'active' | 'deleted'; // 字面量联合
选择建议:
- 优先使用
interface(尤其对公共 API),因为它支持声明合并 - 需要联合类型/映射类型时用
type
2.3 泛型(Generics)
// 泛型函数
function identity<T>(arg: T): T {
return arg;
}
identity<string>('hello'); // 显式指定
identity(42); // 类型推断:T = number
// 泛型接口
interface ApiResponse<T> {
data: T;
status: number;
message: string;
}
// 使用
const userResponse: ApiResponse<User> = {
data: { id: 1, name: 'Alice' },
status: 200,
message: 'OK',
};
2.4 类型推断
TypeScript 有强大的类型推断能力,大部分场景不需要显式声明类型:
let count = 0; // TS 推断为 number
let user = { name: 'Alice' }; // 推断为 { name: string }
// 函数返回值推断
function getUser() {
return { id: 1, name: 'Alice' };
}
// 返回值自动推断为 { id: number; name: string }
三、编译配置(tsconfig.json)
{
"compilerOptions": {
"target": "ES2022", // 编译目标 JS 版本
"module": "ESNext", // 模块系统
"moduleResolution": "bundler", // 模块解析策略
"strict": true, // 启用所有严格类型检查(强烈推荐)
"esModuleInterop": true, // 兼容 CommonJS/ESM 混用
"skipLibCheck": true, // 跳过 node_modules 类型检查
"forceConsistentCasingInFileNames": true,
"outDir": "./dist", // 编译输出目录
"rootDir": "./src", // 源码根目录
"resolveJsonModule": true, // 允许 import JSON
"declaration": true, // 生成 .d.ts 类型声明文件
"noImplicitAny": true, // 禁止隐式 any
"strictNullChecks": true, // 严格 null/undefined 检查
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
严格模式("strict": true)包含:
| 选项 | 说明 |
|---|---|
noImplicitAny | 禁止隐式 any |
strictNullChecks | null/undefined 必须显式处理 |
strictFunctionTypes | 函数参数类型严格逆变 |
strictBindCallApply | bind/call/apply 严格类型检查 |
strictPropertyInitialization | 类属性必须初始化 |
noImplicitThis | this 类型必须显式 |
alwaysStrict | 严格模式编译 |
建议:新项目一律开启 strict: true。已有 JS 迁移项目可以逐步开启。
四、现代 TypeScript 特性(5.0+)
// Decorators(装饰器,Nest.js/Angular 核心语法)
function Controller(prefix: string) {
return function (target: any) {
target.prefix = prefix;
};
}
@Controller('/users')
class UserController {}
// satisfies 操作符(保持类型推断的同时检查兼容性)
const config = {
host: 'localhost',
port: 3000,
} satisfies { host: string; port: number };
// const 类型参数(自动推断为字面量类型)
function createArray<const T>(items: readonly T[]): T[] {
return [...items];
}
const arr = createArray([1, 2, 3] as const); // T = 1 | 2 | 3
五、TypeScript 生态工具
| 工具 | 用途 |
|---|---|
| tsc | TS 编译器 |
| ts-node | 直接运行 TS 文件(开发用) |
| tsx | 更快的 ts-node 替代品 |
| Zod | 运行时类型验证(弥补 TS 只在编译时检查) |
| Prisma | 类型安全的数据库 ORM |
| trpc | 端到端类型安全 API |
常见问题(FAQ)
TypeScript 和 JavaScript 是什么关系?
TS 是 JS 的超集:所有有效的 JS 代码都是有效的 TS 代码。TS 添加了类型系统,编译后生成纯 JS。运行时没有 TS,只有 JS。
学习 TypeScript 需要多久?
- 有 JS 基础:1-2 周能写出类型安全的业务代码
- 掌握泛型/条件类型等高级特性:1-2 个月
- 成为类型体操选手:持续学习
“any” 可以随便用吗?
不要滥用 any,它会让 TS 失去意义。如果发现需要 any,先用 unknown 替代,再通过类型收窄处理。
相关阅读
- TypeScript JS 项目迁移指南
- TypeScript 高级类型详解
- TypeScript Node.js 后端工程化
- TypeScript 严格模式配置与最佳实践
- TypeScript 运行时验证:Zod 实战
- React + TypeScript 实战
- TypeScript 专题导航
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。
「frontend」更多文章
Vue 性能优化指南:虚拟列表、懒加载、渲染优化与 Core Web Vitals
Vue 3 应用性能优化完整策略:虚拟滚动(vue-virtual-scroller)、组件懒加载与异步组件、KeepAlive 缓存、Suspense 异步优化、v-memo 渲染记忆化、响应式性能(shallowRef/toRaw)、Bundle 分析与代码分割、渲染函数优化、以及 Core Web Vitals(LCP/INP/CLS)调优。
Nuxt.js 完全指南:Vue 全栈框架的 SSR、SSG、API 路由与部署实践
Nuxt.js 3 深度实践:文件系统路由、SSR/SSG/ISR 渲染模式、API 路由(Server Routes)、useFetch/useAsyncData 数据获取、中间件与插件、SEO 与 Meta 管理、Nitro 服务端引擎、自动导入、部署到 Vercel/Netlify/Node.js。
Vue 测试深度指南:Vitest + Vue Test Utils + Playwright E2E 与 CI 集成
Vue 3 应用从单元测试到 E2E 的完整测试策略:Vitest + Vue Test Utils 组件测试(mount/emits/slots/async)、Pinia Store Mocking、MSW API 拦截、Playwright 端到端测试、Cypress 组件测试、覆盖率标准与 GitHub Actions CI 集成。