Skip to content
Sarj Code Standards

prefer-total-union-record

Declare a total record for closed registries indexed by a finite string union.

Why

A string index permits a table to omit a union member, so adding a state can silently reach a fallback.

Fix

Use satisfies Record<Key, Value> with the domain union, and supply an entry for each member. Review any fallback separately.

Examples

Before — flagged A string index permits a missing state
label.ts
import type { State } from "./states.js";
const LABELS: Record<string, string> = { failed: "Failed", passed: "Passed" };
export const label = (state: State) => LABELS[state] ?? "Pending";
states.ts
export type State = "failed" | "passed" | "running";
After — preferred A total record requires every state
label.ts
import type { State } from "./states.js";
const LABELS = {
failed: "Failed",
passed: "Passed",
running: "Pending",
} as const satisfies Record<State, string>;
export const label = (state: State) => LABELS[state];
states.ts
export type State = "failed" | "passed" | "running";