Break Union Types
👨💼 You've mastered union types and narrowing!
You learned:
- 🔀 Union types model "one of these types"
- 🔍 Narrowing with
typeof,in, and type guards - 🏷️ Discriminated unions (algebraic data types) with tag properties
- ✅ Exhaustiveness checking to handle all cases
🦉 Discriminated unions are a core functional programming pattern:
type ApiResponse =
| { status: 'loading' }
| { status: 'success'; data: User[] }
| { status: 'error'; error: string }
// The type defines EXACTLY which states are possible.
// No "loading with an error", no "success without data".
// Invalid states are unrepresentable!
This pattern—making impossible states impossible—is used extensively in React,
Redux, and functional programming in general. It's how you let the type system
catch bugs before they happen.
Next up: Literal types—working with exact values!
Test Your Knowledge
Retrieval practice helps solidify learning by actively recalling information. Use this prompt with your AI assistant to quiz yourself on what you've learned.
Please quiz me on exercise 2 using the epicshop MCP server. Call the get_quiz_instructions tool with exerciseNumber "2" to get the quiz instructions, then quiz me one question at a time.