Intersection Type (TypeScript)
An intersection type combines several types into one using the `&` symbol. The result must satisfy all of the combined types at once, getting every property from each.
TypeScript
Where a union means 'one of these', an intersection means 'all of these together'. `A & B` is an object that has all of A's members and all of B's members.
Intersections are commonly used to merge interfaces, for example combining a base entity type with extra fields.
Syntax
type T = A & B;Examples
TypeScript
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
const p: Person = { name: "Bo", age: 7 };
console.log(p.name, p.age);Output:
Bo 7