When Exhaustiveness Isn't Needed
You'll learn to recognize scenarios where partial matching is acceptable and explicit exhaustive checks add no value.
The Core Idea
Not every union type requires exhaustive handling. Sometimes you only care about a subset of possible cases, or there's a sensible default action that covers all remaining variants. In these situations, forcing yourself to explicitly handle every single case creates unnecessary boilerplate without improving type safety.
Think of it like a restaurant menu: if you're coding a "vegetarian filter," you don't need to list every meat dish individually—you just skip anything that isn't vegetarian. The exhaustive approach would force you to name every steak, chicken, and fish option explicitly, even though you're ignoring them all the same way.
When to Skip Exhaustiveness Checks
You don't need exhaustive checking when:
- Default behavior is acceptable: A catch-all
elseordefaultcase handles all other variants identically - Partial matching by design: You're intentionally filtering or processing only specific union members
- Non-critical code paths: Logging, UI hints, or optional enhancements where missing a case isn't catastrophic
- Stable union types: The union rarely changes, so the maintenance benefit of exhaustiveness is minimal
For example, if you're displaying icons for different file types but have a generic "document" icon as fallback, there's no benefit to explicitly listing every possible file format—just handle the special cases you care about and use the default for everything else.
Key Takeaway: Exhaustiveness checking is a powerful tool for catching missing cases when adding new variants should force code updates. When unhandled cases can safely fall through to default behavior, explicit exhaustiveness adds complexity without safety benefits.