Course contentsShow
← TypeScript
Lesson 1335 of 1,79631. Type-Driven DesignFree lesson

Loading States Done Wrong

The classic mistake: separate booleans for isLoading, isError, and hasData that create impossible combinations.

Loading States Done Wrong

What you'll learn: How separate boolean flags for loading, error, and data states create impossible and inconsistent application states.

The Classic Mistake

When fetching data, developers often reach for the simplest solution: multiple independent boolean flags. It feels natural—just track everything separately:

interface UserProfile {
  isLoading: boolean;
  isError: boolean;
  hasData: boolean;
  data: User | null;
  error: Error | null;
}

This compiles without complaint. TypeScript happily accepts it. But there's a hidden trap.

The Impossible States Problem

With three independent booleans, you've created 8 possible combinations (2³), but only 3-4 are actually valid:

  • isLoading: true, isError: false, hasData: false ✓ Currently loading
  • isLoading: false, isError: true, hasData: false ✓ Error occurred
  • isLoading: false, isError: false, hasData: true ✓ Success
  • isLoading: false, isError: false, hasData: false ✓ Initial/idle state

But what about:

  • isLoading: true, isError: true, hasData: false ❌ Loading AND errored?
  • isLoading: true, isError: false, hasData: true ❌ Loading AND has data?
  • isLoading: false, isError: true, hasData: true ❌ Error AND has data?

These combinations are nonsensical, yet TypeScript allows them. Your render logic must defensively handle states that should never exist. Bugs hide in these impossible corners—what should display when both isError and hasData are true?

Why This Happens

Independent booleans give TypeScript no way to express mutual exclusivity. The type system can't prevent you from writing state.isLoading = true; state.hasData = true; simultaneously.

Key Takeaway: Multiple independent boolean flags for mutually exclusive states create impossible combinations that the type system cannot prevent, leading to defensive code and hidden bugs.