Skip to content

q-undeclared-error ​

q(...) returns early an error that the function it returns from doesn't declare. On by default, as an error.

Why: q(result) gives TypeScript the value (T), and the transform turns it into "or return the Err now". So tsc checks that the function returns some Result, and nothing checks that the error q returns is one the function declares. A function declaring Result<number, LoadError> that passes on its callee's ParseError returns a value its type says it can't: a caller's match over LoadError has no arm for it, and a switch that covers every declared error falls through. Only safe-ts emit followed by tsc used to find it; this rule finds it where an agent looks, in safe-ts check and in the editor. Guide: Errors.

❌ Rejected: parsePort's PortError leaves connect, which declares only ConnectError

✕ Rejectedq-undeclared-error
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>();
type ConnectError = Enum<{ Refused: { port: number } }>;
const ConnectError = constructors<ConnectError>();

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

export function connect(text: string): Result<number, ConnectError> {
  const port = q(parsePort(text));
  return port === 0 ? err(ConnectError.Refused({ port })) : port;
}
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>();
type ConnectError = Enum<{ Refused: { port: number } }>;
const ConnectError = constructors<ConnectError>();

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

export function connect(text: string): Result<number, ConnectError> {
  const __q1 = parsePort(text); if (__q1 instanceof __qErr) return __q1; const port = __q1;
  return port === 0 ? err(({ tag: "Refused", port } satisfies ConnectError as ConnectError)) : port;
}

✅ Accepted: the error turned into one the function declares, with q's mapper

✓ 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>();
type ConnectError = Enum<{ Refused: { port: number }; BadPort: { problem: PortError } }>;
const ConnectError = constructors<ConnectError>();

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

export function connect(text: string): Result<number, ConnectError> {
  const port = q(parsePort(text), (problem): ConnectError => ConnectError.BadPort({ problem }));
  return port === 0 ? err(ConnectError.Refused({ port })) : port;
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Err as __qErr, mapErr as __qMapErr } 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>();
type ConnectError = Enum<{ Refused: { port: number }; BadPort: { problem: PortError } }>;
const ConnectError = constructors<ConnectError>();

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

export function connect(text: string): Result<number, ConnectError> {
  const __q1 = parsePort(text); if (__q1 instanceof __qErr) return __qMapErr(__q1, (problem): ConnectError => ({ tag: "BadPort", problem } satisfies ConnectError as ConnectError)); const port = __q1;
  return port === 0 ? err(({ tag: "Refused", port } satisfies ConnectError as ConnectError)) : port;
}

✅ Accepted too: the function declares both

✓ Accepted0 diagnostics
ts
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

function parseCount(text: string): Result<number, "bad-number"> {
  const n = Number(text);
  return Number.isInteger(n) ? n : err("bad-number");
}

export function load(text: string): Result<number, "bad-number" | "empty"> {
  if (text === "") return err("empty");
  const n = q(parseCount(text));
  return n + 1;
}
ts
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

function parseCount(text: string): Result<number, "bad-number"> {
  const n = Number(text);
  return Number.isInteger(n) ? n : err("bad-number");
}

export function load(text: string): Result<number, "bad-number" | "empty"> {
  if (text === "") return err("empty");
  const __q1 = parseCount(text); if (__q1 instanceof __qErr) return __q1; const n = __q1;
  return n + 1;
}

What it compares ​

  • The error q passes on is read from what is written, for the call q takes (written in place, awaited, q(await f()), or kept first in a const read once):
    • std's helpers: their error types are std's, unknown for unsafe(...) and unsafeAsync(...), JsonError for parseJson<T>(...), validate<T>(...) and the other JSON readers, NumberError for parseNumber, and so on (a table checked against std's sources by a test, so it reads the same whether std is installed or is the project's code);
    • a pack's safe version (readFile of the Node pack): an error object (SystemError<...>, a DOMException), whose type the package declares and the checker doesn't read: it is no literal and no primitive, which is what can be decided of it;
    • a function of the project, resolved as every rule resolves calls (across files, a method through the receiver's written type, this, a field): the error type E of its declared return type, Result<T, E> or Promise<Result<T, E>>, through aliases in any file. A function with no declared return type gives the literals its return err("...")s give, when every other return returns a value that can't be an Err (a literal, arithmetic, a count .length / .size, an object or a list written there, a const read once holding one);
    • a call through a declared function type (hooks.parse(text) with parse: (text: string) => Result<number, Code>, a parameter, a field): that type's return type;
    • with a mapper, q(result, mapError), what the mapper returns instead: its declared return type (written there, (e): LoadError => ..., or of the function it names, q(r, toLoadError)), or for an arrow written there without one, what it plainly returns (a literal, a template or String(...): a string, a call: its function's declared return type).
  • The error the function declares is the E of its own declared return type, through aliases. The function is the innermost one: in a callback, the callback.
  • Compared are literals ("missing", 404), the primitives (string, number, boolean), the program's Enum types and classes by identity (a subclass of a declared class fits), other named types by identity, the language's error classes (Error, TypeError, ...) by name, and unions of those. unknown on the function's side accepts everything; a literal fits its primitive. A finding names each error that fits nothing declared, where that is decided: a literal or a primitive against named types or classes, an Enum or a class against literals or other Enums and classes, a named type against literals.
  • Not compared (silent): a type parameter on either side, a generic alias, a conditional or mapped type, an object type written in place, a package's type, a named object type against another named type or a global class (they may be compatible), a mapper without a declared return type or passed by name, and a callee the checker can't resolve or that comes from outside the program (a declare function, a package).

What the rule can't see ​

  • A pack's safe version against a named error type: its error type is in the package's declarations, which the checker doesn't read; it is told apart only from literals and primitives (Result<T, "missing">, Result<T, string>), not from a named type it may fit.
  • A function typed by where it is written (a callback whose return type comes from the const's annotation or the parameter it is passed to): only a declared return type is read.
  • Undeclared errors through names: a function with no declared return type that returns a name read more than once or in a branch, a member (other than .length / .size) or a call's value has errors the rule doesn't list, and it isn't compared.
  • Enum types alike in shape: two Enums declared apart are two types here, even where their variants have the same tags and fields and TypeScript would take one for the other.

How far it reaches ​

On an adopter (about 760 files), of its 300 q(...) sites the rule compares both sides at 238: 154 through a function of the project, 44 through a mapper, 21 through a pack's safe version, 12 through a declared function type, 7 through std's helpers. It doesn't at 62: 28 mappers it can't read (an arrow that returns something not plainly one thing, or the function's own type not read), 18 safe versions whose function declares an error type it can't read, 13 calls it can't resolve (a package's function, a method of a value of unknown type) and 3 functions of the project whose types aren't read (a generic). It reports 5, each a safe version's error object passed on by a function declaring string.

Related: q-needs-result-return, q-position, failure-contract (the same question for a thrown PropagateError and a Throws type); guide: Errors.

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