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

正如用户在输出中看到的,两个对象的属性都相同,显示了真值。

广告

© . All rights reserved.