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
qpasses on is read from what is written, for the callqtakes (written in place, awaited,q(await f()), or kept first in aconstread once):- std's helpers: their error types are std's,
unknownforunsafe(...)andunsafeAsync(...),JsonErrorforparseJson<T>(...),validate<T>(...)and the other JSON readers,NumberErrorforparseNumber, 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 (
readFileof the Node pack): an error object (SystemError<...>, aDOMException), 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 typeEof its declared return type,Result<T, E>orPromise<Result<T, E>>, through aliases in any file. A function with no declared return type gives the literals itsreturn err("...")s give, when every otherreturnreturns a value that can't be anErr(a literal, arithmetic, a count.length/.size, an object or a list written there, aconstread once holding one); - a call through a declared function type (
hooks.parse(text)withparse: (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 orString(...): astring, a call: its function's declared return type).
- std's helpers: their error types are std's,
- The error the function declares is the
Eof 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'sEnumtypes 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.unknownon 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, anEnumor a class against literals or otherEnums 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. Enumtypes alike in shape: twoEnums 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.