Utility 类型
一句话解释
Utility 类型是 构建-in TypeScript 类型那个 transform existing 类型 in 常见方式: 部分, 必需, Readonly, 选取, Omit, 记录, and 更多.
用大白话说
Utility types are built-in type transformations. They take an existing type and produce a new one with modified properties. They save you from writing repetitive mapped types manually.
Common utility types include: 部分<T> (all properties optional), 必需<T> (all properties required), Readonly<T> (all properties readonly), 选取<T, K> (select specific properties), Omit<T, K> (remove specific properties), and 记录<K, V> (create an object type with keys K and values V).
These are implemented as mapped types under the hood. Understanding them is essential for writing clean, DRY TypeScript code.
示例
interface User { id: number; name: string; email: string; }
type UserPreview = Pick<User, "id" | "name">;
type UserUpdate = Partial<User>;
type UserRecord = Record<string, User>;选取选择属性, 部分制作所有可选, 记录创建映射.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。