no-throw
throw is only allowed for errors that extend PropagateError.
Why: a plain throw is invisible in the function's signature, so callers can't know they need to handle it. Either return the failure (Result) or throw something the analysis tracks (PropagateError). Guide: Errors.
✕ Rejectedno-throw
ts
export function withdraw(balance: number, amount: number): number {
if (amount > balance) throw new Error("insufficient funds");
return balance - amount;
}✅ Accepted: return the failure
✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export type WithdrawError = Enum<{ InsufficientFunds: { missing: number } }>;
export const WithdrawError = constructors<WithdrawError>();
export function withdraw(balance: number, amount: number): Result<number, WithdrawError> {
if (amount > balance) return err(WithdrawError.InsufficientFunds({ missing: amount - balance }));
return balance - amount;
}ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export type WithdrawError = Enum<{ InsufficientFunds: { missing: number } }>;
export const WithdrawError = constructors<WithdrawError>();
export function withdraw(balance: number, amount: number): Result<number, WithdrawError> {
if (amount > balance) return err(({ tag: "InsufficientFunds", missing: amount - balance } satisfies WithdrawError as WithdrawError));
return balance - amount;
}✅ Also accepted: a tracked error, for a failure that ends the whole request or job, caught once at its top
✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
export class InsufficientFunds extends PropagateError {}
export function withdraw(balance: number, amount: number): number {
if (amount > balance) throw new InsufficientFunds(`needs ${amount - balance} more`);
return balance - amount;
}An error made by one of your own functions can be thrown too, when the function is declared to return a PropagateError class and every return in it is new of that class or of one that extends it. The thrown kind is the declared class, so a catch testing for it handles it:
✅ Accepted: an error factory (a bad field ends the form request; its top answers 400)
✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
export class FieldError extends PropagateError {}
function fieldError(path: string, message: string): FieldError {
return new FieldError(`${path}: ${message}`);
}
export function requireName(name: string): string {
if (name.trim() === "") throw fieldError("name", "must not be empty");
return name.trim();
}Both checks are needed because TypeScript compares classes by shape. A PropagateError subclass with no members of its own has the same shape as every Error, so a factory declared to return it may return a plain Error, or an unrelated class that a catch testing with instanceof would miss:
✕ Rejectedno-throw
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
export class FieldError extends PropagateError {}
function fieldError(message: string): FieldError {
return new Error(message);
}
export function requireName(name: string): string {
if (name === "") throw fieldError("must not be empty");
return name;
}A rejection is a throw by another name, so the same holds for Promise.reject(x) and for the reject(x) of a new Promise executor (or of Promise.withResolvers()): x must be new of a PropagateError subclass, or a factory of yours that makes one. Handing that reject on by name (stream.on("error", reject), pending.set(id, { resolve, reject })) is reported too: whoever calls it chooses what the promise rejects with. Passing on a rejection already raised is a rethrow, and allowed: inner.catch(reject), inner.then(resolve, reject), inner.catch((error) => reject(error)).
✕ Rejectedno-throw
ts
type Stream = { readonly on: (event: "end" | "error", listener: (cause?: unknown) => void) => void };
export function finished(stream: Stream): Promise<void> {
return new Promise((resolve, reject) => {
stream.on("end", () => resolve());
stream.on("error", reject);
});
}
export function load(id: string): Promise<string> {
return id === "" ? Promise.reject(new Error("no id")) : Promise.resolve(id);
}✅ Accepted: the failure is a value the caller handles
✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export type LoadError = Enum<{ NoId: {} }>;
export const LoadError = constructors<LoadError>();
export async function load(id: string): Promise<Result<string, LoadError>> {
if (id === "") return err(LoadError.NoId());
return id;
}ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
export type LoadError = Enum<{ NoId: {} }>;
export const LoadError = constructors<LoadError>();
export async function load(id: string): Promise<Result<string, LoadError>> {
if (id === "") return err(({ tag: "NoId" } satisfies LoadError as LoadError));
return id;
}✅ Also accepted: a tracked error, made where the promise rejects
✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
export class StreamFailed extends PropagateError {}
type Stream = { readonly on: (event: "end" | "error", listener: (cause?: unknown) => void) => void };
export function finished(stream: Stream): Promise<void> {
return new Promise((resolve, reject) => {
stream.on("end", () => resolve());
stream.on("error", (cause) => reject(new StreamFailed(String(cause))));
});
}Rethrowing what a catch caught (throw error with the catch (error) binding, unchanged) is allowed: the analysis knows the try doesn't handle it, so the failure is reported where it starts:
✕ Rejectedunhandled-failure
ts
export function parse(text: string): unknown {
try {
return JSON.parse(text);
} catch (error) {
console.error("bad input", text);
throw error;
}
}Quick fixes: Return err(…) instead of throwing · Throw a PropagateError instead. None for a rejection: returning err(...) in place of Promise.reject(...) would resolve the promise.
What the rule can't see: which class a thrown value has when it isn't new X(...) or a call to such a factory: a value from a variable, a parameter or a package's function, a factory whose return gives anything but new X(...) (flag ? new A() : new B(), a variable), or an async one. Such throws are always reported. A promise's reject called through what it is stored in (pending.get(id)?.reject(new Error("closed"))) isn't seen: it is reported where it is handed on.