A union (A | B) means "either A or B." An intersection (A & B) means "both A and B at once."
ts
= | | ;
: = ;
() { }
A union (A | B) means "either A or B." An intersection (A & B) means "both A and B at once."
= | | ;
: = ;
() { }
function format(x: string | number) {
// x.toFixed(2); // ❌ toFixed doesn't exist on string
if (typeof x === "number") return x.toFixed(2); // ✅ narrowed to number
return x.toUpperCase(); // ✅ here it's a string
}
Until you narrow a union, you can only access members present on every member of the union — the compiler protects you.
type Name = { name: string };
type Age = { age: number };
type Person = Name & Age; // must have BOTH name and age
const p: Person = { name: "Ann", age: 30 }; // both required
Intersections are great for composing/mixing object types (e.g. adding props to an existing type).
Unions model "one of several possibilities" — the foundation of safe state modeling (e.g. discriminated unions for reducer state), function overloads, and nullable types (T | null).
Intersections compose types together.
Together they make TypeScript's type system expressive enough to mirror real data precisely.