开源项目 `utility-types` 使用教程:TypeScript 类型工具库的终极指南
2026-01-18 09:57:19作者:牧宁李
还在为 TypeScript 类型操作烦恼吗?每次都要手动编写复杂的类型工具函数?utility-types 就是你的救星!这个强大的类型工具库提供了 60+ 实用的类型操作工具,让你的 TypeScript 开发体验提升到全新高度。
读完本文,你将掌握:
- 🎯
utility-types的核心功能和使用场景 - 📊 各类类型操作工具的详细用法和示例
- 🔧 实际项目中的最佳实践和技巧
- 🚀 如何避免常见的类型操作陷阱
什么是 utility-types?
utility-types 是一个专为 TypeScript 设计的类型工具库,它提供了丰富的类型操作工具,可以看作是类型层面的 "lodash"。这个库完全在类型层面工作,零运行时开销,让你的代码既安全又高效。
核心特性
| 特性 | 描述 | 优势 |
|---|---|---|
| 类型安全 | 所有工具都经过严格测试 | 避免运行时错误 |
| 零依赖 | 纯 TypeScript 实现 | 项目更轻量 |
| 丰富工具集 | 60+ 类型操作工具 | 覆盖各种场景 |
| Flow 兼容 | 支持 Flow 工具类型迁移 | 平滑迁移体验 |
安装与配置
安装
# 使用 npm
npm install utility-types
# 使用 yarn
yarn add utility-types
TypeScript 版本兼容性
| utility-types 版本 | TypeScript 版本要求 |
|---|---|
| v3.x.x | TypeScript v3.1+ |
| v2.x.x | TypeScript v2.8.1+ |
| v1.x.x | TypeScript v2.7.2+ |
核心工具类型详解
1. 集合操作类型
集合操作类型用于处理联合类型的交集、差集等操作。
import { SetIntersection, SetDifference, SymmetricDifference } from 'utility-types';
// 交集示例
type A = '1' | '2' | '3';
type B = '2' | '3' | '4';
// 结果: "2" | "3"
type IntersectionResult = SetIntersection<A, B>;
// 差集示例
// 结果: "1"
type DifferenceResult = SetDifference<A, B>;
// 对称差集示例
// 结果: "1" | "4"
type SymmetricResult = SymmetricDifference<A, B>;
2. 对象属性操作类型
这些工具帮助你精确控制对象的属性。
import {
FunctionKeys,
NonFunctionKeys,
RequiredKeys,
OptionalKeys
} from 'utility-types';
type User = {
name: string;
age: number;
getName: () => string;
setAge?: (age: number) => void;
};
// 获取函数属性键
// 结果: "getName" | "setAge"
type FunctionProps = FunctionKeys<User>;
// 获取非函数属性键
// 结果: "name" | "age"
type NonFunctionProps = NonFunctionKeys<User>;
// 获取必需属性键
// 结果: "name" | "age" | "getName"
type RequiredProps = RequiredKeys<User>;
// 获取可选属性键
// 结果: "setAge"
type OptionalProps = OptionalKeys<User>;
3. 深度类型操作
处理嵌套对象结构的类型操作。
import { DeepReadonly, DeepPartial, DeepRequired } from 'utility-types';
type NestedUser = {
profile: {
personal: {
name: string;
age?: number;
};
contact: {
email: string;
phone?: string;
};
};
};
// 深度只读
type ReadonlyUser = DeepReadonly<NestedUser>;
// 等价于:
// {
// readonly profile: {
// readonly personal: {
// readonly name: string;
// readonly age?: number;
// };
// readonly contact: {
// readonly email: string;
// readonly phone?: string;
// };
// };
// }
// 深度可选
type PartialUser = DeepPartial<NestedUser>;
// 深度必需
type RequiredUser = DeepRequired<NestedUser>;
4. 高级类型工具
graph TD
A[输入类型] --> B[Unionize]
B --> C[单属性对象联合]
A --> D[Brand]
D --> E[名义类型]
A --> F[ValuesType]
F --> G[值类型联合]
A --> H[PromiseType]
H --> I[Promise解析类型]
import { Unionize, Brand, ValuesType, PromiseType } from 'utility-types';
// Unionize: 将对象转换为单属性对象的联合
type UserProps = { name: string; age: number };
// 结果: { name: string } | { age: number }
type Unionized = Unionize<UserProps>;
// Brand: 创建名义类型
type USD = Brand<number, "USD">;
type EUR = Brand<number, "EUR">;
const usdAmount = 100 as USD;
const eurAmount = 100 as EUR;
// 类型安全 - 这会报错!
// usdAmount = eurAmount; // Error!
// ValuesType: 获取对象值的联合类型
// 结果: string | number
type ValueUnion = ValuesType<UserProps>;
// PromiseType: 获取 Promise 的解析类型
async function fetchUser(): Promise<{ id: number; name: string }> {
return { id: 1, name: "John" };
}
// 结果: { id: number; name: string }
type User = PromiseType<ReturnType<typeof fetchUser>>;
实际应用场景
场景 1: API 响应类型处理
import { DeepPartial, DeepRequired, SetDifference } from 'utility-types';
// API 基础响应类型
type ApiResponse<T> = {
data: T;
status: number;
message?: string;
};
// 用户数据类型
type User = {
id: number;
name: string;
email: string;
profile?: {
avatar?: string;
bio?: string;
};
};
// 创建用户请求类型(所有字段可选)
type CreateUserRequest = DeepPartial<User>;
// 更新用户请求类型(排除 id 字段)
type UpdateUserRequest = SetDifference<DeepPartial<User>, 'id'>;
// 完整的用户响应类型
type UserResponse = DeepRequired<ApiResponse<User>>;
// 使用示例
const createUser = (user: CreateUserRequest): Promise<UserResponse> => {
// API 调用逻辑
};
const updateUser = (id: number, user: UpdateUserRequest): Promise<UserResponse> => {
// API 调用逻辑
};
场景 2: 表单验证类型
import { Optional, Required, Unionize } from 'utility-types';
type UserForm = {
username: string;
email: string;
password: string;
confirmPassword: string;
rememberMe?: boolean;
};
// 登录表单(只需要用户名和密码)
type LoginForm = Required<UserForm, 'username' | 'password'>;
// 注册表单(所有字段必填)
type RegisterForm = Required<UserForm>;
// 忘记密码表单
type ForgotPasswordForm = Optional<UserForm, 'username' | 'rememberMe'>;
// 表单错误类型
type FormErrors = Unionize<{
[K in keyof UserForm]: string;
}>;
// 使用示例
function validateForm<T extends keyof UserForm>(
form: Partial<UserForm>,
field: T
): FormErrors | null {
const value = form[field];
if (!value) {
return { [field]: `${field} is required` } as FormErrors;
}
return null;
}
场景 3: 状态管理类型安全
import { DeepReadonly, Mutable, $Values } from 'utility-types';
// 应用状态类型
type AppState = {
user: {
id: number;
name: string;
preferences: {
theme: 'light' | 'dark';
notifications: boolean;
};
};
loading: boolean;
error?: string;
};
// 只读状态(用于 React 组件)
type ReadonlyState = DeepReadonly<AppState>;
// 可变状态(用于 reducer)
type MutableState = Mutable<AppState>;
// 动作类型
const ActionTypes = {
SET_USER: 'SET_USER',
SET_LOADING: 'SET_LOADING',
SET_ERROR: 'SET_ERROR',
} as const;
type ActionType = $Values<typeof ActionTypes>;
type Action =
| { type: 'SET_USER'; payload: AppState['user'] }
| { type: 'SET_LOADING'; payload: boolean }
| { type: 'SET_ERROR'; payload?: string };
// Reducer 函数
function reducer(state: MutableState, action: Action): MutableState {
switch (action.type) {
case 'SET_USER':
return { ...state, user: action.payload };
case 'SET_LOADING':
return { ...state, loading: action.payload };
case 'SET_ERROR':
return { ...state, error: action.payload };
default:
return state;
}
}
最佳实践与技巧
1. 类型导入策略
// 推荐:按需导入,减少打包体积
import { DeepReadonly, Optional, ValuesType } from 'utility-types';
// 不推荐:整体导入,可能包含未使用的类型
// import * as UtilityTypes from 'utility-types';
2. 组合使用类型工具
import { SetDifference, Optional, DeepPartial } from 'utility-types';
// 创建复杂的类型组合
type ComplexUser = {
id: number;
personalInfo: {
name: string;
age: number;
contacts?: {
email: string;
phone?: string;
};
};
settings: {
theme: string;
notifications: boolean;
};
};
// 创建更新类型(排除 id,所有字段可选)
type UpdateUser = Optional<SetDifference<DeepPartial<ComplexUser>, 'id'>>;
// 结果类型结构:
// {
// personalInfo?: {
// name?: string;
// age?: number;
// contacts?: {
// email?: string;
// phone?: string;
// };
// };
// settings?: {
// theme?: string;
// notifications?: boolean;
// };
// }
3. 避免常见陷阱
// 陷阱 1: 过度使用 Deep 类型
// 对于浅层对象,使用内置类型更高效
type ShallowUser = { name: string; age: number };
type ReadonlyShallow = Readonly<ShallowUser>; // 使用内置 Readonly
// 陷阱 2: 忽略性能考虑
// 对于非常深的嵌套结构,考虑简化设计而不是依赖深度类型
// 陷阱 3: 类型过于复杂
// 如果类型工具使代码难以理解,考虑重构为更简单的设计
与其他工具库对比
| 特性 | utility-types | type-fest | ts-toolbelt |
|---|---|---|---|
| 工具数量 | 60+ | 100+ | 200+ |
| 零依赖 | ✅ | ✅ | ✅ |
| 文档质量 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ |
| 学习曲线 | 平缓 | 中等 | 陡峭 |
| 社区活跃度 | 高 | 高 | 中 |
总结
utility-types 是一个强大而实用的 TypeScript 类型工具库,它提供了丰富的类型操作工具,能够显著提升你的开发效率和代码质量。通过本文的学习,你应该已经掌握了:
- 核心概念:理解了 utility-types 的设计哲学和优势
- 实用工具:掌握了各类类型操作工具的使用方法
- 实战应用:学会了在实际项目中应用这些工具
- 最佳实践:了解了如何避免常见陷阱和优化使用方式
无论你是正在构建大型企业应用,还是开发个人项目,utility-types 都能为你的 TypeScript 开发体验带来质的飞跃。开始使用它,让你的类型代码更加优雅、安全、高效!
💡 提示:记得在项目中合理使用这些工具,避免过度工程化。合适的工具用在合适的地方,才能发挥最大价值。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
LongCat-AudioDiT-1BLongCat-AudioDiT 是一款基于扩散模型的文本转语音(TTS)模型,代表了当前该领域的最高水平(SOTA),它直接在波形潜空间中进行操作。00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
HY-Embodied-0.5这是一套专为现实世界具身智能打造的基础模型。该系列模型采用创新的混合Transformer(Mixture-of-Transformers, MoT) 架构,通过潜在令牌实现模态特异性计算,显著提升了细粒度感知能力。Jinja00
FreeSql功能强大的对象关系映射(O/RM)组件,支持 .NET Core 2.1+、.NET Framework 4.0+、Xamarin 以及 AOT。C#00
项目优选
收起
deepin linux kernel
C
27
14
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
659
4.26 K
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.54 K
894
Ascend Extension for PyTorch
Python
503
609
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
391
285
暂无简介
Dart
905
218
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
昇腾LLM分布式训练框架
Python
142
168
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
939
862
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.33 K
108