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

Modeling Domain Concepts in Types

Translating business rules and domain logic into TypeScript type definitions.

Modeling Domain Concepts in Types

What you'll learn: How to translate business rules and domain logic directly into TypeScript's type system so invalid states become impossible to represent.


When you model domain concepts in types, you're encoding the actual rules of your business problem into the structure of your data. Instead of writing runtime checks everywhere, you design types that make incorrect data unrepresentable.

Think of it like building with LEGO blocks that only fit together in valid ways. Rather than building anything and then checking if it's correct, you design the blocks so they can't connect incorrectly in the first place.

From Loose Types to Domain Models

Consider a payment system. A weak approach uses primitives:

interface Payment {
  status: string;
  amount: number;
  refundAmount: number;
}

This accepts nonsense: a "pending" payment with a refund amount, or a negative amount. The types lie about what's actually valid.

Encoding Business Rules

A domain-driven approach uses the type system to enforce reality:

type PendingPayment = {
  status: "pending";
  amount: number;
};

type CompletedPayment = {
  status: "completed";
  amount: number;
  completedAt: string;
};

type RefundedPayment = {
  status: "refunded";
  amount: number;
  refundAmount: number;
  refundedAt: string;
};

type Payment = PendingPayment | CompletedPayment | RefundedPayment;

Now the compiler enforces that refunds only exist on refunded payments. You've made illegal states impossible to construct.

The Shift in Thinking

Instead of "what data do I need?" ask "what states can exist in my domain?" Model the valid transitions, the constraints, and the relationships as discriminated unions, literal types, and precise interfaces.

Key Takeaway: Type-driven design means translating business invariants into type constraints, so your compiler becomes a domain expert that catches logic errors at compile time.