An intersection type combines the members of multiple TypeScript types. Write `Person & Employee` when an object must satisfy both shapes: it needs every required property from each. Unlike a union, which permits access only to common members, an intersection permits access to members from both types. section 10.3
Combine two object shapes
In `src/index.ts`: `type Person = { id: string; name: string; city: string };` `type Employee = { company: string; dept: string };` `type EmployedPerson = Person & Employee;` `const person: EmployedPerson = { id: "bsmith", name: "Bob", city: "London", company: "Acme Co", dept: "Sales" };` The object has all five required properties. It can also be passed to a function expecting just `Person` or just `Employee`. section 10.3 section 10.3.2
Intersections are useful when data from separate sources must become one usable object. To correlate people and employees, match their shared `id`, then use object spread to produce combined objects. If an employee is missing, supply defaults for `company` and `dept` so the result still satisfies `Person & Employee`. TypeScript can infer that result shape even without the explanatory annotations. section 10.3.1
Check overlapping member names before combining shapes:
- Properties with the same name and type, such as `id: string`, merge without changing their type. section 10.3.2
- Properties with the same name but incompatible types are intersected. If one shape requires `contact: number` and another requires `contact: string`, the resulting property has type `never`; assigning either a number or a string fails. section 10.3.2
- Object-valued properties can combine requirements: `contact: { phone: number }` and `contact: { name: string }` require both fields. Methods with different parameter types retain both callable signatures. section 10.3.2
