Intersection Order Independence
What you'll learn: Why A & B produces the exact same type as B & A, and when ordering might still matter in practice.
The Core Idea
In TypeScript, intersection types are commutative—meaning the order of types in an intersection doesn't change the resulting type. A & B is identical to B & A. This is different from how you might think about some operations where order matters (like subtraction: 5 - 3 isn't the same as 3 - 5).
Think of intersections like mixing ingredients into a bowl. Whether you add sugar then flour, or flour then sugar, you end up with the same mixture in the bowl.
Why Order Doesn't Change the Type
When TypeScript creates an intersection type, it's asking: "What shape must satisfy all of these type requirements?" The result is the same regardless of which requirement you list first:
type PersonName = { name: string };
type PersonAge = { age: number };
type PersonA = PersonName & PersonAge;
type PersonB = PersonAge & PersonName;
// Both are identical: { name: string; age: number }
const p1: PersonA = { name: "Alice", age: 30 };
const p2: PersonB = { name: "Bob", age: 25 };
Both PersonA and PersonB require an object with both name and age properties.
When Order Still Matters Practically
While the type is identical, order can affect:
- Error messages: TypeScript may mention the first type in certain error contexts
- Readability:
BaseConfig & SpecificOptionsreads more naturally than the reverse - Documentation: Listing the "base" type first often makes intent clearer
Key Takeaway: Intersection types are mathematically commutative—A & B equals B & A—but choose an order that makes your code's intent clear to human readers.