Union and intersection types help you combine different types in TypeScript. They let you describe values that can be one type or another (union) or must have all types together (intersection).
Union vs intersection mental model in Typescript
type UnionType = TypeA | TypeB; type IntersectionType = TypeA & TypeB;
The | symbol means 'or' (union).
The & symbol means 'and' (intersection).
type StringOrNumber = string | number;
name and an age.type Person = { name: string } & { age: number };type A = { a: string };
type B = { b: number };
type C = A | B; // union
// C can be either { a: string } or { b: number }
type D = A & B; // intersection
// D must have both { a: string } and { b: number }This example shows how union lets you accept either a cat or a dog, while intersection requires both cat and dog features.
type Cat = { meow: () => void };
type Dog = { bark: () => void };
// Union: can be Cat or Dog
function speak(pet: Cat | Dog) {
if ('meow' in pet) {
pet.meow();
} else {
pet.bark();
}
}
// Intersection: must be both Cat and Dog
function speakBoth(pet: Cat & Dog) {
pet.meow();
pet.bark();
}
const cat: Cat = { meow: () => console.log('Meow!') };
const dog: Dog = { bark: () => console.log('Bark!') };
const catDog: Cat & Dog = {
meow: () => console.log('Meow!'),
bark: () => console.log('Bark!')
};
speak(cat); // Meow!
speak(dog); // Bark!
speakBoth(catDog); // Meow!
Bark!Union types let you work with values that can be different types, but you often need to check which one it is before using it.
Intersection types combine multiple types into one, so the value must have all properties from each type.
Think of union as a choice between types, and intersection as a combination of types.
Union (|) means a value can be one type or another.
Intersection (&) means a value must have all combined types.
Use union for flexible inputs, intersection for combining features.
Practice
union type in TypeScript?Solution
Step 1: Understand union type meaning
A union type means a value can be one of several types, for example,string | numbermeans the value can be either a string or a number.Step 2: Compare with intersection type
An intersection type requires a value to have all combined types, which is different from union. So a value can be one type or another, likestring | number, correctly describes union.Final Answer:
A value can be one type or another, likestring | number. -> Option BQuick Check:
Union = one of multiple types [OK]
- Confusing union with intersection
- Thinking union requires all types simultaneously
- Mixing up syntax symbols | and &
A and B?Solution
Step 1: Recall intersection syntax
Intersection types use the ampersand&symbol to combine types, meaning the value must satisfy both types.Step 2: Identify correct syntax
type C = A & B;usesA & B, which is the correct syntax for intersection types in TypeScript.Final Answer:
type C = A & B; -> Option DQuick Check:
Intersection uses & symbol [OK]
- Using | instead of & for intersection
- Using invalid operators like + or comma
- Confusing type alias syntax
value after this assignment?type A = { name: string };
type B = { age: number };
let value: A | B = { name: "Alice", age: 30 };Solution
Step 1: Understand union type assignment
The variablevalueis declared asA | B, meaning it can be either type A or type B.Step 2: Check assigned object
The assigned object has bothnameandageproperties, which does not strictly satisfy either A or B alone, so it is not valid as a union type.Final Answer:
Error: value must be either A or B, not both. -> Option AQuick Check:
Union requires value to match one type exactly [OK]
- Thinking union allows objects with all properties combined
- Confusing union with intersection requirements
- Assuming extra properties are allowed in union
type A = { name: string };
type B = { age: number };
let person: A & B = { name: "Bob" };Solution
Step 1: Understand intersection type requirements
An intersection typeA & Brequires the value to have all properties from both types A and B.Step 2: Check assigned object properties
The object assigned has onlynameproperty but is missingage, so it does not satisfy the intersection type.Final Answer:
Missing propertyagein the object assigned toperson. -> Option CQuick Check:
Intersection requires all properties present [OK]
- Thinking intersection allows partial properties
- Confusing union and intersection requirements
- Ignoring missing properties error
type Name = { name: string };
type Age = { age: number };
type Contact = { email: string };
// Which type correctly represents a value that must have a name and either age or email?
// Choose the correct type for Person:Solution
Step 1: Analyze requirement
The value must have anameproperty and either anageoremailproperty.Step 2: Translate to types
This means the type must beNameAND (eitherAgeORContact), which isName & (Age | Contact).Step 3: Check options
type Person = Name & (Age | Contact);matches this exactly. Other options either change the logic or require all three properties.Final Answer:
type Person = Name & (Age | Contact); -> Option AQuick Check:
Name AND (Age OR Contact) = Name & (Age | Contact) [OK]
- Ignoring parentheses changing logic
- Mixing order of union and intersection
- Choosing all properties instead of either/or
Ready to prove it?
Pass the test and this pattern moves unknown
