Skip to content

prefer-q ​

if (isErr(x)) return x; and isErr(x) ? x : ...: a failure passed up by hand. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: q(...) is Rust's ?: the build turns const value = q(make()) into exactly that if. One line per step instead of two keeps the steps of a function readable, and a codebase that writes both forms has two ways of saying the same thing. Guide: Errors.

✕ Rejectedprefer-q
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function port(text: string): Result<number, PortError> {
  const n = Number(text);
  return Number.isInteger(n) ? n : err(PortError.NotAPort({ text }));
}

export function range(from: string, to: string): Result<number, PortError> {
  const start = port(from);
  if (isErr(start)) return start;
  const end = port(to);
  if (isErr(end)) return end;
  return end - start;
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function port(text: string): Result<number, PortError> {
  const n = Number(text);
  return Number.isInteger(n) ? n : err(({ tag: "NotAPort", text } satisfies PortError as PortError));
}

export function range(from: string, to: string): Result<number, PortError> {
  const start = port(from);
  if (isErr(start)) return start;
  const end = port(to);
  if (isErr(end)) return end;
  return end - start;
}
✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function port(text: string): Result<number, PortError> {
  const n = Number(text);
  return Number.isInteger(n) ? n : err(PortError.NotAPort({ text }));
}

export function range(from: string, to: string): Result<number, PortError> {
  const start = q(port(from));
  const end = q(port(to));
  return end - start;
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

type PortError = Enum<{ NotAPort: { text: string } }>;
const PortError = constructors<PortError>();

function port(text: string): Result<number, PortError> {
  const n = Number(text);
  return Number.isInteger(n) ? n : err(({ tag: "NotAPort", text } satisfies PortError as PortError));
}

export function range(from: string, to: string): Result<number, PortError> {
  const __q1 = port(from); if (__q1 instanceof __qErr) return __q1; const start = __q1;
  const __q2 = port(to); if (__q2 instanceof __qErr) return __q2; const end = __q2;
  return end - start;
}

q needs the transform (the bundler plugin, the Node loader or the Bun plugin) and a function that returns a Result (q-needs-result-return). A branch that does more than return (logs, cleans up) isn't reported. A re-wrapped error is prefer-map-err.

Not reported in a file that runs without the transform: one that vite.config.* or vitest.config.* imports, also a config in another directory of the project (apps/web/vite.config.ts importing ../../scripts/reload.ts; needs-transform). There the if is the form to write, and q would throw "q() was not compiled" when the config loads, so the fix isn't offered either; a re-wrapped error there is still prefer-map-err, whose help names mapErr and not q.

Quick fix: Use const x = q(...), where the Result is made by the const just above and the function declares a Result return type; it adds the import of q.

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