liveBook · Manning

Intersection type

As explained in Essential TypeScript 5, Third Edition

An intersection type is a TypeScript type that combines multiple types into one type requiring their combined members.

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

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through syntax, constituent compatibility, overlapping properties, incompatible types, nested shapes, and method signatures.
Examples
Uses Person and Employee objects, an `id`-based array correlation, default employee fields, and conflicting `contact` properties.
What it assumes
Assumes familiarity with JavaScript objects while developing TypeScript object shapes and their combinations.
What you can do after
Build it from scratch by defining combined object types and supplying every required member, including defaults when correlated data is missing.
How it connects
Links intersections to object shape types, type aliases, unions, structural compatibility, object spread, and type inference.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.