Discover how union and intersection types can clear up your confusion and make your code smarter!
Union vs intersection mental model in Typescript - When to Use Which
Imagine you have a box of different colored pens and a box of different colored markers. You want to pick items that are either pens or markers, or sometimes only those that are both pens and markers. Doing this by sorting each item manually is confusing and slow.
Manually checking each item to see if it belongs to one group or both is tiring and easy to mess up. You might forget some items or mix categories, leading to mistakes and wasted time.
Using union and intersection types in TypeScript helps you clearly define whether you want items that belong to one group or both. This makes your code simpler, safer, and easier to understand.
function handle(item: Pen | Marker) { if (item.type === 'pen') { /* pen logic */ } else if (item.type === 'marker') { /* marker logic */ } }type PenMarker = Pen & Marker; // item must be both pen and markerYou can precisely describe and work with complex data shapes, making your programs smarter and less error-prone.
Think of a user profile that can be a 'Guest' or 'Member' (union), but sometimes you need a user who is both a 'Member' and an 'Admin' (intersection) to allow special access.
Union types let you accept one type or another.
Intersection types require all combined types together.
They help organize and clarify your code logic.
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
