Q07. What is the difference between a union and an intersection?
A union A | B means a value may satisfy either alternative; only safe shared members are available before narrowing. An intersection A & B requires a value to satisfy both types. Use unions for alternatives and intersections for compatible composition.
A union models different states; an intersection combines requirements. Conflicting intersection properties can create an impossible type.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
Union and intersection
type A = { id: string };
type B = { active: boolean };
type Both = A & B;💡 Both requires id and active, whereas A | B permits either alternative.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
Which type requires a value to satisfy both A and B?