Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 8
Intersection Types - Combining Types
An intersection type (`&`) creates a type that satisfies ALL combined types simultaneously. Where union says 'one of', intersection says 'all of'. Intersections are commonly used to compose object types, add traits to base types, and build mixin patterns.
Intersection Types - Composition Patterns
// ── Basic intersection ────────────────────────────────────────
interface Named { name: string; }
interface Aged { age: number; }
type Person = Named & Aged;
const person: Person = { name: "Alice", age: 30 }; // must have BOTH
// ── Extending types with intersection ─────────────────────────
interface BaseEntity {
readonly id: number;
createdAt: Date;
updatedAt: Date;
}
interface TaskFields {
title: string;
status: "todo" | "done";
priority: "low" | "high";
}
type Task = BaseEntity & TaskFields;
// Task must have: id, createdAt, updatedAt, title, status, priority
// ── Intersection best practice: use interface extends instead ──
// For object-to-object extension, interface extends is semantically cleaner:
interface Task extends BaseEntity, TaskFields {}
// Use intersection (&) when composing TYPE ALIASES, not interfaces:
type AdminTask = Task & { adminNote: string };
// ── Mixing in behaviour with intersection ──────────────────────
type WithTimestamps<T> = T & {
createdAt: Date;
updatedAt: Date;
};
type UserWithTimestamps = WithTimestamps<{ name: string; email: string }>;
// ── Conflict resolution in intersections ──────────────────────
type A = { value: string };
type B = { value: number };
type AB = A & B;
// AB.value is type: string & number = never
// (a value cannot be both string and number - the type is impossible)
const broken: AB = { value: "x" }; // ❌ ErrorCommon Mistakes
- Using `&` when you need `|` - intersection means ALL, union means ANY ONE. Mixing them up produces impossible types (string & number = never).
- Ignoring that conflicting primitive properties produce `never` - if two intersected types define the same property with different primitive types, that property becomes `never` and the object cannot exist.
- Overusing intersection for extending - prefer `interface extends` for clarity when extending object types. Reserve `&` for composing type aliases and generic utility patterns.
Tip
Tip
Practice Intersection Types Combining Types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Intersection Types - Combining Types
Practice Task
Note
Practice Task - (1) Write a working example of Intersection Types Combining Types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Intersection Types Combining Types is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.
Key Takeaways
- An intersection type (`&`) creates a type that satisfies ALL combined types simultaneously.
- Using `&` when you need `|` - intersection means ALL, union means ANY ONE. Mixing them up produces impossible types (string & number = never).
- Ignoring that conflicting primitive properties produce `never` - if two intersected types define the same property with different primitive types, that property becomes `never` and the object cannot exist.
- Overusing intersection for extending - prefer `interface extends` for clarity when extending object types. Reserve `&` for composing type aliases and generic utility patterns.
Finished this lesson?