0Pricing
TypeScript Academy · Lesson

Intersection Types: A and B

Combine types with & to merge their properties.

Intersection Types: A and B is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Intersection types combine multiple types into one. A value of an intersection type must satisfy ALL of the combined types simultaneously.

Basic Intersection Syntax

Use the `&` operator to create an intersection. The result has all properties of both types.
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
const alice: Person = { name: 'Alice', age: 30 };

All Properties Required

An intersection requires ALL properties from all combined types. Missing any property is a type error.
type A = { x: number };
type B = { y: number };
type AB = A & B;
// const p: AB = { x: 1 }; // Error — missing y

Merging Object Types

Intersections are often used to merge two object types that don't extend each other.
type Serializable = { serialize(): string };
type Loggable = { log(): void };
type Service = Serializable & Loggable;

Intersection vs Extends

Interface extension and intersection produce similar results for object types, but intersection works with type aliases including unions and primitives.
// Interface extend:
interface C extends A, B {}

// Intersection type:
type D = A & B;
// Both give the same shape for simple objects

Intersection with Conflicting Types

If two intersected types have the same property with conflicting types, the result is `never` for that property.
type X = { id: string };
type Y = { id: number };
type XY = X & Y;
// XY.id is string & number = never — impossible!

Intersection in Function Parameters

Accept an argument that must satisfy multiple interface contracts using an intersection.
function process(item: Serializable & Loggable): void {
  item.log();
  console.log(item.serialize());
}

Mixin Pattern with Intersections

Intersections enable the mixin pattern — combining behaviors from multiple sources.
type Timestamped<T> = T & { createdAt: Date };
type User = Timestamped<{ name: string; email: string }>;

Primitive Intersections Collapse to never

Intersecting primitive types that cannot overlap collapses to `never`.
type T = string & number; // never — a value can't be both

Intersection vs Union: The Core Difference

Union (|) means OR — the value can be any one of the types. Intersection (&) means AND — the value must satisfy all the types.

Using Intersections with Generics

Combine a generic type with additional properties using an intersection for flexible, extensible types.
type WithId<T> = T & { id: number };
const user: WithId<{ name: string }> = { id: 1, name: 'Alice' };

Quick Check

What does `type T = string & number` evaluate to?

Recap

Intersection types (&) require ALL properties from all combined types. Use them to merge object shapes, implement mixins, and combine generic types. Conflicting primitive intersections collapse to never.

Frequently asked questions

Is the “Intersection Types: A and B” lesson free?

Yes — the full text of “Intersection Types: A and B” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Intersection Types: A and B”?

Combine types with & to merge their properties. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Intersection Types: A and B” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Union Types: A or B
  2. Intersection Types: A and B
  3. Discriminated Unions for Safe Pattern Matching
  4. Practical Patterns with Union and Intersection
← Back to TypeScript Academy