Skip to content

string-dispatch ​

One plain string (not a union of literals) compared with three or more words in one function: an if chain, || tests, switch cases. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: the set of commands, modes or kinds then exists only as these tests. Any other list of it (a help text, a menu, a second function that reads the same word) is written separately and drifts: a command is added to the ifs and not to the help, an alias works in one place and not in the other. Nothing can check that a string compared in one place is handled in another. Parse the string once, where it comes in, into a closed type, and every place after that is checked: exhaustive-switch and exhaustive-dispatch then report a command left out. Guide: Enums.

❌ Rejected: the commands exist only as ifs

✕ Rejectedstring-dispatch
ts
type Launcher = { readonly urls: readonly string[] };

const HELP = "open, urls, help";

declare function openAll(urls: readonly string[]): void;
declare function banner(urls: readonly string[]): void;
declare function say(text: string): void;

export function run(word: string, launcher: Launcher): void {
  if (word === "open") return openAll(launcher.urls);
  if (word === "urls") return banner(launcher.urls);
  if (word === "help" || word === "?") say(HELP);
}

✅ Accepted: a table parses the word, aliases included; the help is made from it, and match fails to compile when a command has no handler

✓ Accepted0 diagnostics
ts
import { getOwn } from "@evobug_com/safe-ts-std/dict";
import { match } from "@evobug_com/safe-ts-std/enum";
import type { Option } from "@evobug_com/safe-ts-std/result";

type Launcher = { readonly urls: readonly string[] };

const COMMANDS = { open: "open", urls: "urls", help: "help", "?": "help" } as const;
type Command = (typeof COMMANDS)[keyof typeof COMMANDS];

const HELP = Object.keys(COMMANDS).join(", ");

declare function openAll(urls: readonly string[]): void;
declare function banner(urls: readonly string[]): void;
declare function say(text: string): void;

export function parseCommand(word: string): Option<Command> {
  return getOwn(COMMANDS, word);
}

export function run(word: string, launcher: Launcher): void {
  return match(parseCommand(word), {
    open: () => openAll(launcher.urls),
    urls: () => banner(launcher.urls),
    help: () => say(HELP),
    undefined: () => say(`unknown command: ${word}`),
  });
}
ts
import { getOwn } from "@evobug_com/safe-ts-std/dict";
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { match } from "@evobug_com/safe-ts-std/enum";
import type { Option } from "@evobug_com/safe-ts-std/result";

type Launcher = { readonly urls: readonly string[] };

const COMMANDS = { open: "open", urls: "urls", help: "help", "?": "help" } as const;
type Command = (typeof COMMANDS)[keyof typeof COMMANDS];

const HELP = Object.keys(COMMANDS).join(", ");

declare function openAll(urls: readonly string[]): void;
declare function banner(urls: readonly string[]): void;
declare function say(text: string): void;

export function parseCommand(word: string): Option<Command> {
  return getOwn(COMMANDS, word);
}

export function run(word: string, launcher: Launcher): void {
  { const __ms1 = parseCommand(word); let __mv1: void; switch (__ms1) {
    case "open": { __mv1 = openAll(launcher.urls); break; }
    case "urls": { __mv1 = banner(launcher.urls); break; }
    case "help": { __mv1 = say(HELP); break; }
    case undefined satisfies typeof __ms1: { __mv1 = say(`unknown command: ${word}`); break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }

}

What counts ​

  • Reported: a value whose type is string (or Option<string>), written the same way each time (word, args.mode), compared with === or ==, or in switch cases, with three or more distinct string literals that contain a letter, in one function (nested functions count on their own). Reported once, at the first comparison.
  • Not reported: "" (a test for an empty string), literals without a letter (a scanner comparing a character with "("), typeof x === "...", and a value typed as a union of literals (the other two rules check those). Nor a function declared to return a union of literals, an Enum, or an Option, Result or Promise of one: that function is the parser at the boundary, and comparing the string is its job.

What the rule can't see ​

A value whose type the code doesn't say (a function's result that isn't declared nor plain, what is read off, a package's value such as an event's key) is left alone. Comparisons spread over several functions are counted per function.

Why it is opt-in: code that dispatches on strings works until the lists drift, and moving it to a closed type is a refactor, not a mechanical fix.

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