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

The Impossible States Problem

Why naive type modeling allows invalid combinations that should never exist in your domain.

The Impossible States Problem

What you'll learn: How naive type modeling can allow invalid combinations of properties that should never exist together in your domain.

The Problem

When you model data with types, it's tempting to define all possible properties independently. This creates impossible states—combinations of values that make no sense in your domain but TypeScript happily accepts.

Think of a traffic light: it can be red, yellow, or green, but never two colors at once. If you model this with three separate boolean properties (isRed, isYellow, isGreen), you've allowed 8 possible combinations when only 3 are valid. The other 5 states are impossible in reality but perfectly legal to TypeScript.

A Classic Example

interface Request {
  loading: boolean;
  error: string | null;
  data: string | null;
}

// All of these are valid to TypeScript:
const impossible1: Request = { loading: true, error: "Failed", data: "Success" };
const impossible2: Request = { loading: false, error: null, data: null };
const impossible3: Request = { loading: true, error: "Failed", data: null };

In reality, a request should be in exactly one state: loading, failed (with error), or succeeded (with data). But our type allows nonsense like "loading with both error and data" or "not loading with nothing."

Why This Matters

These impossible states force defensive code everywhere. Every function handling a Request must ask: "What if both error and data exist?" Your types should make these questions impossible to ask.

Key Takeaway: Independent boolean and nullable fields often create invalid combinations. Type-driven design means making impossible states unrepresentable in your type system.