Skip to content

exhaustive-dispatch ​

An if / else if chain, a run of if (...) return ...;, a chain of ?: or a switch that compares one value with two or more members of a closed set, and doesn't handle the others. A closed set is:

  • an enum's tag (s.tag === "Circle", is(s, "Circle"));
  • a union of string or number literals ("red" | "amber" | "green");
  • the constants of a code object: an as const or frozen object of literals, such as a codesOf(...) table (JSON). The set comes from the value's type, or, when the value is typed number or string, from the object every tested constant is read from (u.phase === Phase.WALK).

The value's type can allow fewer members than a whole table: p.op of a union of packet types, each with one code of an Op table as its op, is one of those packets' codes, also where the union is derived from a table of packets ({ [O in OpCode]: ... }[OpCode]). Only those codes are asked for, named by the table's constants (Op.Ping). The same for a member of a class instance declared Mut<C> or Readonly<C>: its declared type, as for C.

Why: the branches name the members one by one, and the last else (often a 0, an undefined, or nothing) takes the rest. That holds while the set doesn't change. When a member is added, it lands in that else without a word, in every chain at once: a new TEND phase got its branch in one function, and two others quietly computed 0 for it. A chain that handles every member is reported the day a member is added, and so is the place to update. Guide: Enums.

❌ Rejected: a phase added later gets 0

ts
export const Phase = Object.freeze({ WALK: 0, RUN: 1, TEND: 2 });
export type Phase = (typeof Phase)[keyof typeof Phase];

type Unit = { readonly phase: Phase; readonly speed: number };

export function step(u: Unit): number {
  return u.phase === Phase.WALK ? u.speed : u.phase === Phase.RUN ? u.speed * 2 : 0;
}

✅ Accepted: a switch that TypeScript checks

✓ Accepted0 diagnostics
ts
import { unreachable } from "@evobug_com/safe-ts-std/enum";

export const Phase = Object.freeze({ WALK: 0, RUN: 1, TEND: 2 });
export type Phase = (typeof Phase)[keyof typeof Phase];

type Unit = { readonly phase: Phase; readonly speed: number };

export function step(u: Unit): number {
  switch (u.phase) {
    case Phase.WALK:
      return u.speed;
    case Phase.RUN:
      return u.speed * 2;
    case Phase.TEND:
      return 0;
    default:
      return unreachable(u.phase);
  }
}

✅ Accepted: a table, which TypeScript checks for every member

✓ Accepted0 diagnostics
ts
export const Phase = Object.freeze({ WALK: 0, RUN: 1, TEND: 2 });
export type Phase = (typeof Phase)[keyof typeof Phase];

type Unit = { readonly phase: Phase; readonly speed: number };

const SPEED: Readonly<Record<Phase, number>> = { [Phase.WALK]: 1, [Phase.RUN]: 2, [Phase.TEND]: 0 };

export function step(u: Unit): number {
  return u.speed * SPEED[u.phase];
}

For an enum or a union of strings, match is the short form, with _ where the rest really share a branch:

ts
import type { Enum } from "@evobug_com/safe-ts-std/enum";

type Shape = Enum<{ Circle: { r: number }; Square: { side: number }; Dot: {} }>;

export function area(s: Shape): number {
  if (s.tag === "Circle") return Math.PI * s.r ** 2;
  if (s.tag === "Square") return s.side ** 2;
  return 0;
}
✓ Accepted0 diagnostics
ts
import { type Enum, match } from "@evobug_com/safe-ts-std/enum";

type Shape = Enum<{ Circle: { r: number }; Square: { side: number }; Dot: {} }>;

export function area(s: Shape): number {
  return match(s, {
    Circle: ({ r }) => Math.PI * r ** 2,
    Square: ({ side }) => side ** 2,
    Dot: () => 0,
  });
}
ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { type Enum, match } from "@evobug_com/safe-ts-std/enum";

type Shape = Enum<{ Circle: { r: number }; Square: { side: number }; Dot: {} }>;

export function area(s: Shape): number {
  { const __ms1 = s; let __mv1: number; switch (__ms1.tag) {
    case "Circle": { const { r } = __ms1; __mv1 = Math.PI * r ** 2; break; }
    case "Square": { const { side } = __ms1; __mv1 = side ** 2; break; }
    case "Dot": { __mv1 = 0; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }

}
ts
export type Light = "red" | "amber" | "green";

export function wait(light: Light): number {
  return light === "red" ? 30 : light === "amber" ? 5 : 0;
}
✓ Accepted0 diagnostics
ts
import { match } from "@evobug_com/safe-ts-std/enum";

export type Light = "red" | "amber" | "green";

export function wait(light: Light): number {
  return match(light, { red: () => 30, amber: () => 5, green: () => 0 });
}
ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { match } from "@evobug_com/safe-ts-std/enum";

export type Light = "red" | "amber" | "green";

export function wait(light: Light): number {
  { const __ms1 = light; let __mv1: number; switch (__ms1) { case "red": { __mv1 = 30; break; } case "amber": { __mv1 = 5; break; } case "green": { __mv1 = 0; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }
}

What counts as a dispatch ​

  • Reported: two or more branches, each testing members of the same value with === or == (several joined by || count as one branch: x === A || x === B), where some member has no branch and the chain doesn't end in unreachable(x). The final else doesn't matter: a neutral default, a computed value and no else at all are reported alike, because each of them hides the member that is added later.
  • A branch narrowed by && (x === A && ready) names A too, when the comparison comes first and another branch is a plain comparison. What matters is the member no branch names: an adopter's if (u.phase === Phase.WALK) … else if (u.phase === Phase.CLEAN && …) … else if (u.phase === Phase.INSIDE) … had no branch for a farmer's TEND, and a world with one in it stopped loading.
  • Not reported: one test with an else (x === "red" ? "#f00" : "#0f0", is(s, "Circle") ? ... : ...). There the other members really are "everything else", which is what is, variant and _ are for (Enums). Nor a chain that handles every member, or ends in unreachable(x); nor tests of different values, a chain with any other test in it (ready && x === A, if (s) …), or one whose every branch is narrowed (x === A && y === id), which are filters rather than a dispatch.
  • A switch on a value typed as the set is exhaustive-switch's: it asks for default: unreachable(x). This rule reports a switch on a value typed number or string whose cases are the constants of one object (case Phase.WALK:), where unreachable(x) can't compile: handle every constant, or type the value as the set.

Members the code before it took ​

Only the members that can get to the chain are asked for: what TypeScript has narrowed away can't be tested again without an error ("This comparison appears to be unintentional", TS2367). The rule follows the same tests as exhaustive-switch: an earlier if (x === A) return; (or x === A || x === B, a const holding the test, a call of std's unreachable or panic, also renamed, through a const copy, or as .call / .apply), an enclosing if (x !== A), && or ?: branch, a switch on the value before it whose case A returns, and the case of an enclosing switch (x) the chain is in, where only that case's labels (and those falling into it) get. The message then says how many members can get there (tests some of the 3 values that can reach it (of the 32 of its type)), lists only those it doesn't handle, and doesn't say "and so would a member added later" where the enclosing case keeps a new member out. The same rules decide when a test no longer counts (an assignment, a loan, an await, ...).

An adopter's ?: chain inside an eight-label case of a switch on a command's type was reported for the 24 command types that case doesn't let in.

✅ Not reported: only "move" and "stop" get to the ?: chain

✓ Accepted0 diagnostics
ts
import { unreachable } from "@evobug_com/safe-ts-std/enum";

export type Command = "move" | "stop" | "build" | "sell";

export function priority(command: Command): number {
  switch (command) {
    case "move":
    case "stop":
      return command === "move" ? 2 : command === "stop" ? 1 : 0;
    case "build":
      return 3;
    case "sell":
      return 4;
    default:
      return unreachable(command);
  }
}

What the rule can't see ​

The set comes from the declared types (a parameter, a field, a type in another file) and from const objects of literals, also a table built by a function such as codesOf(names) whose parameter is names: T & Check<T>, and a packet type built from a table (PacketOf<O> = O extends Code ? Simplify<{ op: O } & …> : never), whose op is read without building the other fields. A value whose type the code doesn't say (a function's result that is neither declared nor plain, a package's value) is left alone. A test the rule doesn't read (x !== A, which is negative-member-guard's; [A, B].includes(x), typeof x === "...", a comparison with a variable) makes the chain no dispatch. The narrowing before a chain is followed within one function: a test outside the callback the chain is in counts for nothing, and unreachable-code still reads the declared type. A Vue template's v-if chains are vue-exhaustive-v-if's.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra