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

Order Independence

Intersection order doesn't matter: A & B is equivalent to B & A, producing the same merged type.

Order Independence in Intersection Types

You'll learn: In intersection types, the order of the combined types doesn't affect the result—A & B is the same as B & A.

The Core Idea

When you create an intersection type, you're merging multiple types together. Unlike some operations in programming where order matters (like subtraction or string concatenation), intersection types are commutative. This means TypeScript treats A & B and B & A as identical types.

Think of it like mixing paint colors or combining ingredients in a recipe where order doesn't matter. Whether you add blue to yellow or yellow to blue, you still get green. Similarly, whether you write Person & Employee or Employee & Person, you get the same merged type with all properties from both.

Why This Matters

This makes your code more flexible and predictable:

type WithName = { name: string };
type WithAge = { age: number };

// These are exactly the same type
type Person1 = WithName & WithAge;
type Person2 = WithAge & WithName;

const p1: Person1 = { name: "Alice", age: 30 };
const p2: Person2 = { name: "Bob", age: 25 };

// They're completely interchangeable
const p3: Person1 = p2; // ✓ Works perfectly
const p4: Person2 = p1; // ✓ Also works perfectly

You can organize your intersections in whatever order makes your code most readable, without worrying about changing the type's behavior.

Key Takeaway: Intersection type order is irrelevant—A & B and B & A produce identical merged types, giving you freedom to organize your type combinations for maximum clarity.