We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Intersections of Types

An intersection type combines multiple types into one with the & operator. The resulting intersection type satisfies all the component types simultaneously.

type IndividualContributor = {
  id: number;
  name: string;
  tasks: string[];
};

type Manager = {
  directReports: number[];
};

type GoodManager = IndividualContributor & Manager;

const hunter: GoodManager = {
  id: 1,
  name: "Hunter Backmann",
  tasks: ["Fixing Lane's B*llsh*t code", "Vibe Coding"],
  directReports: [2, 3, 4],
};

A GoodManager must have all the properties of both an IndividualContributor and a Manager. When you intersect object types, TypeScript merges their properties:

type Point2D = {
  x: number;
  y: number;
};

type Point3D = Point2D & {
  z: number;
};

// Equivalent to:
// type Point3D = {
//   x: number;
//   y: number;
//   z: number;
// };

Assignment

Create and export new TextBot and VoiceBot types. They should each intersect with the existing SupportBot type but have the following additional properties:

  • TextBot:
    • messageLog - an array of strings
    • sendMessage - a function that takes a message string and returns a string
  • VoiceBot
    • callLog - an array of strings
    • phoneNumber - a string
    • dialNumber - a function that takes a phoneNumber string and returns a string