Lesson 103 of 296
Union, intersection and literal types
What you will learn
- Union types: "this OR that"
- Literal unions for fixed choices
- Intersection types: "this AND that"
- What you can do with a union value
On this page
Union types
A union type means a value can be one of several types. It is written with |.
let id: string | number;
id = 42; fine
id = "A-42"; fine
id = true; ErrorLiteral unions
A union of literal types lists the exact allowed values. This replaces "magic strings" and is one of the most useful features in TypeScript:
type Status = "pending" | "paid" | "shipped" | "cancelled";
function setStatus(status: Status) { }
setStatus("paid"); fine
setStatus("payed"); Error: typo caughtYour editor also auto-completes the allowed values.
type Size = "sm" | "md" | "lg";
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
type Dice = 1 | 2 | 3 | 4 | 5 | 6;Using a union value
On a union, you can only use what every member supports:
function show(id: string | number) {
id.toUpperCase(); Error: 'toUpperCase' does not exist on type 'number'.
id.toString(); fine: both strings and numbers have toString
}To use type-specific features, narrow first (Lesson 106):
function show(id: string | number) {
if (typeof id === "string") {
return id.toUpperCase();
}
return id.toFixed(0);
}Nullable values
null and undefined are just members of a union:
let currentUser: User | null = null;With strict null checks (Lesson 105), TypeScript makes you handle the null case before using the value.
Intersection types
An intersection type means a value must be all of the types at once. It is written with &.
type HasId = { id: number };
type HasTimestamps = { createdAt: Date; updatedAt: Date };
type Product = HasId & HasTimestamps & {
name: string;
pricePence: number;
};A Product must have id, createdAt, updatedAt, name and pricePence.
Intersections are used to combine object types. If two types conflict completely (like string & number), the result is never.
Union vs intersection: the surprise
With objects, the names can feel backwards:
- A union
A | Bvalue is either an A or a B, so you can only safely use the properties they share. - An intersection
A & Bvalue has all properties of both.
type Cat = { name: string; meow(): void };
type Dog = { name: string; bark(): void };
function f(pet: Cat | Dog) {
pet.name; fine: both have it
pet.meow(); Error: a Dog has no meow
}Common mistakes
- Plain
stringwhere a literal union would catch typos. - Using a member-specific property on a union without narrowing.
- Using
&when you meant "one of these" (|).
How to say it in an interview
"A union means a value is one of several types, and on a union I can only use what all members share until I narrow it. Literal unions like 'pending' | 'paid' replace magic strings and give autocomplete and typo checking. An intersection means a value satisfies all types at once, which I use to compose object types; conflicting primitives intersect to never."
Key words
| Word | Meaning |
|---|---|
| Union | A | B: one of several types |
| Literal union | A union of exact values |
| Intersection | A & B: all of the types together |
| Nullable | A type that includes null or undefined |
| Magic string | A hard-coded string with special meaning, easy to mistype |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.