Intersections of Types
What
An intersection type combines multiple types into one. The resulting type satisfies all the component types simultaneously, meaning an object of this type must have all properties from every intersected type.
How
You use the ampersand (&) operator to merge types. This is particularly common when combining multiple object definitions:
type Point2D = { x: number; y: number; };
type Point3D = Point2D & { z: number; };
const p: Point3D = {
x: 10,
y: 20,
z: 30
};
Why
Intersections allow you to build complex types from simpler, reusable building blocks. They are often used to extend existing types with additional properties or to combine multiple constraints into a single requirement.
Summary
Intersections are used to say a type must be “this AND that,” merging multiple requirements into a single definition.