返回课程

类型工具

编程代码 · TypeScript

💡 TypeScript 内置工具类型:Partial、Readonly、Pick、Omit 等
type Partial<T> = { [P in keyof T]?: T[P] };// Partial<T> 将所有属性变为可选(自定义实现)
type ReadOnly = Readonly<User>;// Readonly<User> 将 User 所有属性变为只读
type Names = Pick<User, 'name' | 'email'>;// Pick 从 User 中挑选 name 和 email 两个属性
type WithoutId = Omit<User, 'id'>;// Omit 从 User 中排除 id 属性
type Status = 'active' | 'inactive' | 'pending';// 联合类型:Status 只能是这三个字符串之一
0 字/分0% 准确
🐵
t左手食指
ESC 暂停 退格