Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Combining Types  ›  Lesson

Intersection Types

Intersection Types 12 minAn intersection type A & B requires a value to be both A and B at once, so it has every property of both.

Intersection Types

Tasks, projects and comments all need the same two timestamps: createdAt and updatedAt. Copying those lines into every type is tedious and easy to get wrong. An intersection lets you write them once and bolt them onto anything.

The rule

A & B means both at once: every property of each.

An intersection type joins types with &. A value must satisfy all of them together:

type Task = { id: number; title: string };
type Timestamps = { createdAt: string; updatedAt: string };

type StoredTask = Task & Timestamps;

const t: StoredTask = {
  id: 1,
  title: "Buy milk",
  createdAt: "2026-09-01",
  updatedAt: "2026-09-02",
};

StoredTask has all four properties. Leave any one out and it is an error, because the value would no longer be both a Task and a Timestamps.

Compare with a union. The names feel backwards at first:

  • Union A | B — the value is one or the other, so you can safely use fewer properties (only the shared ones).
  • Intersection A & B — the value is one and the other, so it has more properties (all of them).

What if both sides define the same property? Its types are intersected too:

type A = { id: number };
type B = { id: string };
type C = A & B;   // id: number & string, which is never

No value is both a number and a string, so number & string is never, and no object can satisfy C. When you see never appear unexpectedly, look for a clash like this.

Intersections work with any types, not only objects, and they compose: Task & Timestamps & { ownerId: number } is perfectly normal. Interfaces offer a similar tool with extends, which you will meet in the last section.

The shortcut

Read & as "plus" for object types: Task & Timestamps is "a Task plus timestamps". Then check for clashes: "Does any property name appear on both sides with different types?"

Building a shared taskStep 1 of 4
  1. Start with the pieces

    Task, Timestamps, and a new { ownerIds: number[] }.

Drills

Answer fast — trust the rule.

quick drill

How many properties must a value of { a: number } & { b: string } have?

quick drill

What is string & number?

quick drill

type StoredTask = Task & Timestamps. Where can a StoredTask be used?

quick drill

Which description matches a union rather than an intersection?

Remember

  • A & B means both at once: every property of each.
  • Read & as "plus", then check for property names that clash.
  • Trap: the same property with different types on each side becomes never.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainStacking shapes

You have type Task = { id: number; title: string } and type Timestamps = { createdAt: string; updatedAt: string }. Explain what Task & Timestamps contains, and what goes wrong with type Weird = { id: number } & { id: string }.