In TypeScript, intersection types combine multiple types into a single type. An object of this type must include all the properties from the merged types. The & operator is used to define intersection types, making them useful when a value is required to satisfy several type contracts simultaneously.
Syntax:
type CombinedType = TypeA & TypeB;
let variable: CombinedType;
In the above syntax:
- & is used to create an intersection type.
- CombinedType merges TypeA and TypeB.
- Any variable of CombinedType must include all properties from both types.
- This ensures the variable satisfies both type contracts at the same time.
Example 1: Creating an intersected type
interface Student {
student_id: number;
name: string;
}
interface Teacher {
Teacher_Id: number;
teacher_name: string;
}
type intersected_type = Student & Teacher;
let obj1: intersected_type = {
student_id: 3232,
name: "rita",
Teacher_Id: 7873,
teacher_name: "seema",
};
console.log(obj1.Teacher_Id);
console.log(obj1.name);
Output:
7873
ritaIn this example:
- Two interfaces, Student and Teacher, are defined.
- An intersection type is created using the & operator between Student and Teacher.
- The intersection type includes all properties from both interfaces.
- An object of the intersection type is created, which must define every property from Student and Teacher.
- Properties cannot be accessed unless they are properly assigned in the intersection type object.
Example 2:
interface A {
feauA: string;
feauB: string;
}
interface B {
feauA: number;
feauB: string;
}
type AB = A & B;
let obj1: AB;
let obj2: AB;
// Error, Type '20' is not assignable
// to type 'string & number'
obj1.feauA = 20;
console.log(obj1.feauA);
obj2.feauB = "c";
console.log(obj2.feauB);
Output:
error TS2322: Type 'number' is not assignable to type 'never'.
obj1.feauA = 20;
// Error, Type '20' is not assignable
// to type 'string & number'In this example:
- Two interfaces, A and B, are created.
- Both interfaces have a property named feauA, but with different types (string in one and number in the other).
- When these interfaces are combined using an intersection type, the property feauA becomes string & number.
- Since no value can be both a string and a number, assigning 20 causes a TypeScript error.
- Assigning a string does not raise an error, because the compiler interprets the intersection as compatible with string.
Example 3: Intersection types are commutative and associative
commutative property: A&B = B&A
associative property: (A&B)&C = A&(B&C)interface A {
prop1: String;
}
interface B {
prop2: String;
}
interface C {
prop3: String;
}
let obj1: A & B = { prop1: "length", prop2: "width" };
let obj2: B & A = { prop1: "length", prop2: "width" };
let obj3: A & (B & C) = { prop1: "", prop2: "", prop3: "" };
let obj4: (A & B) & C = { prop1: "", prop2: "", prop3: "" };
obj3.prop3 = "height";
console.log(obj3.prop3);
obj4.prop1 = "length";
console.log(obj4.prop1);
console.log(obj3 == obj4); // false
console.log(typeof obj3 == typeof obj4); // true
console.log(typeof obj1 == typeof obj2); // true
Output:
height
length
false
true
trueIn this example:
- The order of intersection does not affect the result when combining two or more types.
- Changing the order of types in an intersection still produces the same intersected type.
- The typeof operator can be used to confirm that the types remain identical.
- The properties of the intersected objects also remain the same, regardless of order.