TypeScript 实用工具类型

TypeScript 实用工具类型

Partial
Readonly
Record
Pick
Omit
Exclude

Partial

作用: 生成一个新类型 该类型和 T 拥有相同的属性 但是所有属性皆为可选项
(构造类型)Type 将 Type 的所有属性设置为可选 该类型将返回表示输入类型的所有字类型

Partial<Type>
Constructs a type with all properties of Type set to optional. 
This utility will return a type that repressents all subsets of a given type.

构造类型的所有属性都设置为可选的类型。
此实用程序将返回一个表示给定类型的所有子集的类型。
interface Person {
	name: string;
	age: number;
}
type anothor = Partial<Person>;
// {name?: string, age?: number}
function updateObject<T>(obj: T, props: Partial<T>) {
	return { ...obj, ...props };
}
updateObject<Person>(person, {name: "李四"});

Readonly

作用: 生成一个新的类型 T 中的 K 属性是只读的 K 属性是不可修改的
(构造类型)Type 把 Type 的所有属性设置为 readonly 意味着不能重新赋值

Constructs a type with all properties of Type set to readonly, 
meaning the properties of the constructed type cannot be reassigned.

构造类型的所有属性都设置为只读的类型,这意味着无法重新分配构造类型的属性
把类型中的属性都变成只读的  接收类型  返回类型
Readonly<Type>
interface Person {
	name: string;
	age: number;
}
// {readonly name: string, readonly age: number}

const anthor: Readonly<Person> = {
	name: "李四",
	age: 40,
}

// 不可以修改
// anthor.name = "张三"

Record

作用: 定义一个对象的 key 和 value 类型
构造一个类型 其属性名的类型为 Keys 属性值得类型为 Type
这个工具可用来把某个类型的属性映射到另一个类型上

Constructs an object type whose property keys are Keys and whose property values are Type. 
This utility can be used to map the properties of a type to another type.
Record<Keys, Type>
// 字典
let employees = {
	1: {id: 1, fullname: "John Doe", role: "Designer"},
	2: {id: 2, fullname: "Ibrahima Fall", role: "Developer"},
	3: {id: 3, fullname: "Sara Duckson", role: "Developer"}
}
interface EmployeeType {
	id: number,
	fullname: string,
	role: string,
}

let employees: Record<number, EmployeeType> = {
	1: {id: 1, fullname: "John Doe", role: "Designer"},
	2: {id: 2, fullname: "Ibrahima Fall", role: "Developer"},
	3: {id: 3, fullname: "Sara Duckson", role: "Developer"}
}

Pick

作用: 生成一个新的类型 该类型拥有 T 中的 K 属性集 新类型 相当于 T 和 K 的交集

从类型 Type 中 挑选部分属性 keys 来构造类型

Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type.

通过从类型中选取一组属性键(字符串文字或字符串文字的并集)来构造类型。
Pick<Type, Keys>
interface Todo {
	title: string;
	description: string;
	completed: boolean;
}

/*
	type TodoPreview = {
		title: string;
		completed: boolean;
	}
*/
// Pick 单词意为  选择
type TodoPreview = Pick<Todo, "title" | "completed">

Omit

作用: 生成一个新类型 该类型拥有 T 中除了 K 属性以外的所有属性

接收类型 得到新类型 在新类型中不包含 keys
从类型 Type 中获取所有属性 然后从中剔除 Keys 属性后构造一个类型

Constructs a type by picking all properties from Type and then removing Keys (string literal or union of string literals).
Omit<Type, Keys>
interface Todo {
	title: string;
	description: string;
	completed: boolean;
}
/*
	type TodoPreview = {
		title: string;
		completed: boolean;
	}
*/
type TodoPreview = Pick<Todo, "title" | "completed">;

Exclude

作用: 如果 T 是 U 的子类型 返回 never 不是返回 T

从类型 Type 中剔除所有可以赋值给 ExcludedUnion 的属性 然后构造一个类型

Constructs a type by excluding from Type all union members that are assignable to ExcludedUnion.
Exclude<UnionType, ExcludedMembers>
// type T0 = "b" | "c"
Type T0 = Exclude<"a" | "b" | "c" | "a" | "b">;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值