- TypeScript 基础
- TypeScript - 首页
- TypeScript - 路线图
- TypeScript - 概述
- TypeScript - 环境设置
- TypeScript - 基本语法
- TypeScript 与 JavaScript
- TypeScript - 特性
- TypeScript - 变量
- TypeScript - let & const
- TypeScript - 运算符
- TypeScript 基本类型
- TypeScript - 类型
- TypeScript - 类型注解
- TypeScript - 类型推断
- TypeScript - 数字
- TypeScript - 字符串
- TypeScript - 布尔值
- TypeScript - 数组
- TypeScript - 元组
- TypeScript - 枚举
- TypeScript - Any
- TypeScript - Never
- TypeScript - 联合类型
- TypeScript - 字面量类型
- TypeScript - 符号
- TypeScript - null 与 undefined
- TypeScript - 类型别名
- TypeScript 控制流
- TypeScript - 决策制定
- TypeScript - If 语句
- TypeScript - If Else 语句
- TypeScript - 嵌套 If 语句
- TypeScript - Switch 语句
- TypeScript - 循环
- TypeScript - For 循环
- TypeScript - While 循环
- TypeScript - Do While 循环
- TypeScript 函数
- TypeScript - 函数
- TypeScript - 函数类型
- TypeScript - 可选参数
- TypeScript - 默认参数
- TypeScript - 匿名函数
- TypeScript - 函数构造器
- TypeScript - Rest 参数
- TypeScript - 参数解构
- TypeScript - 箭头函数
- TypeScript 接口
- TypeScript - 接口
- TypeScript - 扩展接口
- TypeScript 类和对象
- TypeScript - 类
- TypeScript - 对象
- TypeScript - 访问修饰符
- TypeScript - 只读属性
- TypeScript - 继承
- TypeScript - 静态方法和属性
- TypeScript - 抽象类
- TypeScript - 访问器
- TypeScript - 鸭子类型
- TypeScript 高级类型
- TypeScript - 交叉类型
- TypeScript - 类型守卫
- TypeScript - 类型断言
- TypeScript 类型操作
- TypeScript - 从类型创建类型
- TypeScript - Keyof 类型运算符
- TypeScript - Typeof 类型运算符
- TypeScript - 索引访问类型
- TypeScript - 条件类型
- TypeScript - 映射类型
- TypeScript - 模板字面量类型
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型约束
- TypeScript - 泛型接口
- TypeScript - 泛型类
- TypeScript 其他
- TypeScript - 三斜杠指令
- TypeScript - 命名空间
- TypeScript - 模块
- TypeScript - 环境声明
- TypeScript - 装饰器
- TypeScript - 类型兼容性
- TypeScript - 日期对象
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 实用程序类型
- TypeScript - 装箱和拆箱
- TypeScript - tsconfig.json
- 从 JavaScript 到 TypeScript
- TypeScript 有用资源
- TypeScript - 快速指南
- TypeScript - 有用资源
- TypeScript - 讨论
TypeScript - 交叉类型
在 TypeScript 中,交叉类型将多个类型组合成一个。虽然 TypeScript 中的交叉类型和联合类型相似,但它们的使用方式却大不相同。将不同类型组合成一个的类型称为交叉类型。这使您可以组合多个类型以生成具有所有必需属性的单个类型。此类型的对象将包含每个提供的类型中的成员。交叉类型使用“&”运算符创建。
当 TypeScript 中的两种类型相交时,交叉类型将继承两个相交类型的特征。在组合具有相同属性名称但类型不同的类型时要小心。
语法
我们可以在 TypeScript 中编写以下语法来创建交叉类型。
type intersepted_Type = Type1 & Type2;
示例
在下面的示例中,我们创建了两个名为“Book”和“Author”的接口。现在在 Book 中,我们创建了两个名为“book_id”的字段,它是数字类型,以及“book_name”,它是字符串类型。在 Author 中,我们还创建了两个名为“author_id”的字段,它是数字类型,以及“author_name”,它是字符串类型。接下来,我们对 Book 和 Author 接口进行了交叉,并将其存储到 intersected_types 中。最后,从创建的交叉类型对象中检索值。
interface Book {
book_id: number
book_name: string
}
interface Author {
author_id: number
author_name: string
}
type intersected_type = Book & Author
let intersected_type_object1: intersected_type = {
book_id: 101,
book_name: 'Typescript is Awesome',
author_id: 202,
author_name: 'Tutorialspoint!',
}
console.log('Book Id: ' + intersected_type_object1.book_id)
console.log('Book name: ' + intersected_type_object1.book_name)
console.log('Author Id: ' + intersected_type_object1.author_id)
console.log('Author name: ' + intersected_type_object1.author_name)
编译后,它将生成以下 JavaScript 代码:
var intersected_type_object1 = {
book_id: 101,
book_name: 'Typescript is Awesome',
author_id: 202,
author_name: 'Tutorialspoint!'
};
console.log('Book Id: ' + intersected_type_object1.book_id);
console.log('Book name: ' + intersected_type_object1.book_name);
console.log('Author Id: ' + intersected_type_object1.author_id);
console.log('Author name: ' + intersected_type_object1.author_name);
输出
以上代码将产生以下输出:
Book Id: 101 Book name: Typescript is Awesome Author Id: 202 Author name: Tutorialspoint!
正如用户在输出中看到的,两个不同接口的所有值都已组合并显示。
交叉类型是结合律和交换律的
交换律表明,方程式的因子可以在不改变方程式结果的情况下自由重新排列。
commutative property: (A & B) = (B & A)
结合律断言,在运算期间更改整数的分组方式不会影响方程式的解。
associative property: (A & B) & C = A & (B & C)
当我们交叉两个或多个类型时,它们的顺序无关紧要。使用“typeof”运算符来验证交叉对象的属性也相同,无论项目如何交叉或以何种顺序交叉。
示例
正如我们在下面的示例中看到的,这里我们创建了三个名为“Student”,“Class”和“Subject”的接口。现在在 Student 中,我们创建了两个名为“student_id”的字段,它是数字类型,以及“sudent_name”,它是字符串类型。在“Class”实例中,我们还创建了两个名为“class_id”的字段,它是数字类型,以及“class_name”,它是字符串类型。在“Subject”实例中,我们还创建了两个名为“subject_id”的字段,它是数字类型,以及“subject_name”,它是字符串类型。
接下来,我们使用结合律对 Book、Author 和 Subject 接口进行了交叉,并将其存储到 intersected types 中。之后,从创建的交叉类型对象中检索值。最后,我们使用 typeof 运算符检查对象并将其记录到控制台。
interface Student {
student_id: number
student_name: string
}
interface Class {
class_id: number
class_name: string
}
interface Subject {
subject_id: number
subject_name: string
}
type intersected_type_1 = (Student & Class) & Subject
type intersected_type_2 = Student & (Class & Subject)
let intersected_type_object1: intersected_type_1 = {
student_id: 101,
student_name: 'Typescript',
class_id: 10,
}
let intersected_type_object2: intersected_type_2 = {
student_id: 102,
student_name: 'Typescript2',
class_id: 11,
}
console.log(typeof intersected_type_object1 === typeof intersected_type_object2)
编译后,它将生成以下 JavaScript 代码:
var intersected_type_object1 = {
student_id: 101,
student_name: 'Typescript',
class_id: 10
};
var intersected_type_object2 = {
student_id: 102,
student_name: 'Typescript2',
class_id: 11
};
console.log(typeof intersected_type_object1 === typeof intersected_type_object2);
输出
以上代码将产生以下输出:
true
正如用户在输出中看到的,两个对象的属性都相同,显示了真值。