Course contentsShow
← TypeScript
Lesson 456 of 1,79610. Intersection TypesFree lesson

Real-World Intersection Use Cases

Apply common intersection patterns in API typing, event handlers, and component prop composition scenarios.

Real-World Intersection Use Cases

You'll learn: How intersection types solve everyday problems in API typing, event handling, and component composition.

When Intersections Shine in Practice

You've explored intersection syntax and edge cases. Now let's see where they genuinely improve real-world TypeScript code. Intersections excel at combining separate concerns into complete types without creating new named types for every variation.

API Response Typing

APIs often return base data plus metadata. Instead of repeating fields:

type User = {
  id: string;
  name: string;
};

type Timestamped = {
  createdAt: Date;
  updatedAt: Date;
};

type UserResponse = User & Timestamped;

const user: UserResponse = {
  id: "123",
  name: "Alice",
  createdAt: new Date(),
  updatedAt: new Date()
};

You compose types horizontally rather than duplicating properties or forcing inheritance.

Event Handler Composition

Event handlers often need base event data plus domain-specific information:

type BaseEvent = {
  timestamp: number;
  source: string;
};

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

type ClickEvent = BaseEvent & ClickData;

function handleClick(event: ClickEvent): void {
  console.log(event.timestamp, event.x, event.y);
}

Component Props with Features

React-style components benefit from intersection-based prop composition:

type BaseProps = {
  id: string;
  className: string;
};

type InteractiveProps = {
  onClick: () => void;
  disabled: boolean;
};

type ButtonProps = BaseProps & InteractiveProps;

This lets you mix and match capabilities without creating rigid class hierarchies.

Key Takeaway: Use intersections to compose types from independent concerns—especially for API responses, event handlers, and component props where multiple pieces of data naturally combine.