开源项目 `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 开发体验带来质的飞跃。开始使用它,让你的类型代码更加优雅、安全、高效!
💡 提示:记得在项目中合理使用这些工具,避免过度工程化。合适的工具用在合适的地方,才能发挥最大价值。
登录后查看全文
热门项目推荐
相关项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
热门内容推荐
最新内容推荐
Degrees of Lewdity中文汉化终极指南:零基础玩家必看的完整教程Unity游戏翻译神器:XUnity Auto Translator 完整使用指南PythonWin7终极指南:在Windows 7上轻松安装Python 3.9+终极macOS键盘定制指南:用Karabiner-Elements提升10倍效率Pandas数据分析实战指南:从零基础到数据处理高手 Qwen3-235B-FP8震撼升级:256K上下文+22B激活参数7步搞定机械键盘PCB设计:从零开始打造你的专属键盘终极WeMod专业版解锁指南:3步免费获取完整高级功能DeepSeek-R1-Distill-Qwen-32B技术揭秘:小模型如何实现大模型性能突破音频修复终极指南:让每一段受损声音重获新生
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
532
3.75 K
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
67
20
暂无简介
Dart
772
191
Ascend Extension for PyTorch
Python
340
405
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
886
596
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
23
0
React Native鸿蒙化仓库
JavaScript
303
355
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
336
178