TypeScript 复杂类型与函数全解析
1. 类型交集(Intersection Types)
类型交集处理相对简单,因为可以对其执行所有成员类型都支持的操作。例如,对于 Person 和 PhysicalObject 这两个类型的交集,能对其应用任何接受 Person 或 PhysicalObject 作为参数的函数,因为这个交集是 Person 和 PhysicalObject 的子类型。
下面是 Person 和 PhysicalObject 接口的定义:
interface Person {
readonly name: string;
readonly secondName?: string;
readonly surname: string;
}
interface PhysicalObject {
weight: number;
height: number;
width: number;
length: number;
}
type PhysicalPerson = Person & PhysicalObject;
PhysicalPerson 必须同时具备 Person 和 Phy
超级会员免费看
订阅专栏 解锁全文
1889

被折叠的 条评论
为什么被折叠?



