Skip to content

Illegal states

M1 built Relay’s config loader. Now Relay needs a model of its core data: jobs. A job is queued, then running on a worker, then it either fails with a reason or succeeds. Each state has its own facts: only a running job has a worker, only a failed job has a reason.

The quick way to write that is one object with a status and a pile of optional fields. It compiles, and it lets you write jobs that can’t exist. This lesson replaces it with a type where those jobs are a compile error.

interface Job {
id: string
status: "queued" | "running" | "failed" | "succeeded"
queuedAt: number
startedAt?: number
worker?: string
finishedAt?: number
reason?: string
}
// Nonsense, and it compiles: a queued job that already has a worker and a finish time.
const queued: Job = { id: "job_1", status: "queued", queuedAt: 0, worker: "w1", finishedAt: 5 }
// A failed job with no reason.
const failed: Job = { id: "job_2", status: "failed", queuedAt: 0 }

Every reader now has to defend itself. Is worker set? Is reason there when status is "failed"? The type can’t say, so each function re-checks, and one forgotten check prints failed: undefined in a customer’s alert. The bug isn’t in any one function. It’s in the type.

Write one interface per state. Each lists exactly the fields that state has, and status is a literal type that names the state:

interface Queued {
readonly id: string
readonly status: "queued"
readonly queuedAt: number
}
interface Running {
readonly id: string
readonly status: "running"
readonly queuedAt: number
readonly startedAt: number
readonly worker: string
}
interface Failed {
readonly id: string
readonly status: "failed"
readonly queuedAt: number
readonly startedAt: number
readonly worker: string
readonly finishedAt: number
readonly reason: string
}
interface Succeeded {
readonly id: string
readonly status: "succeeded"
readonly queuedAt: number
readonly startedAt: number
readonly worker: string
readonly finishedAt: number
}
type Job = Queued | Running | Failed | Succeeded

Nothing is optional now. A Failed job always has a reason, and a Queued job can’t have a worker because the type has no such field.

id and queuedAt repeat in every state. Put them in a base interface and extend it:

interface JobBase {
readonly id: string
readonly queuedAt: number
}
interface Queued extends JobBase {
readonly status: "queued"
}
interface Running extends JobBase {
readonly status: "running"
readonly startedAt: number
readonly worker: string
}
interface Failed extends JobBase {
readonly status: "failed"
readonly startedAt: number
readonly worker: string
readonly finishedAt: number
readonly reason: string
}
interface Succeeded extends JobBase {
readonly status: "succeeded"
readonly startedAt: number
readonly worker: string
readonly finishedAt: number
}
export type Job = Queued | Running | Failed | Succeeded

JobBase is not part of the union. It only saves typing. Job is the four states and nothing else.

On a union you can only touch fields every member has. Check status first, and the compiler narrows job to the matching member (M1’s narrowing, with status as the discriminant):

interface JobBase {
readonly id: string
readonly queuedAt: number
}
interface Queued extends JobBase {
readonly status: "queued"
}
interface Failed extends JobBase {
readonly status: "failed"
readonly reason: string
}
type Job = Queued | Failed
function describe(job: Job): string {
// @ts-expect-error: reason only exists on Failed, and job could be Queued
const early = job.reason
if (job.status === "failed") {
return `${job.id} failed: ${job.reason}` // job is Failed
}
return `${job.id} is ${job.status}` // job is Queued
}

Inside the if, reason is a plain string. No undefined check, because a Failed job can’t lack one.

When you write an object literal for a union, the compiler uses the status value to decide which member you mean, then checks the literal against that member. That includes the excess property check from M1:

interface Queued {
readonly status: "queued"
readonly id: string
}
interface Running {
readonly status: "running"
readonly id: string
readonly worker: string
}
type Job = Queued | Running
const ok: Job = { status: "running", id: "job_1", worker: "w1" }
// @ts-expect-error: status "queued" selects Queued, which has no worker
const bad: Job = { status: "queued", id: "job_1", worker: "w1" }
// @ts-expect-error: status "running" selects Running, which needs a worker
const missing: Job = { status: "running", id: "job_1" }

The bag accepted both of those. The union rejects them where they’re written, not three calls later when a reader trips over them.

Count what each model allows. The bag has four optional fields, and each is either present or absent, so there are 2⁴ = 16 field combinations. Multiply by four statuses and the type allows 64 shapes. Four of them make sense.

The union allows exactly four shapes, one per status, and all four make sense. Each new optional field doubles the bag’s count. A fifth state adds one shape to the union.

Don’t split a field that’s valid in every state. id and queuedAt live in the base because every job has them, and a job that has no queuedAt isn’t a job. Split only when a field’s presence depends on the state. If two fields always appear together (startedAt and worker here), that’s a hint they belong to the same states, not a reason for more types.

Job below is a bag of optionals, and durationMs and summary check for undefined. Replace Job with the union and rewrite both functions by narrowing on status. The tests include lines that must not compile, so the starter fails until Job is a real union.

  • A status plus optional fields allows combinations that can’t happen, and every reader has to re-check them.
  • One interface per state, with exactly that state’s fields, makes the invalid combinations unwritable.
  • Shared fields go in a base interface that you extend. The base is not part of the union.
  • Narrow on status to read state-specific fields. Literals are checked against the member their status selects.
  • Split a field out only when its presence depends on the state.