implicit-propagation
A method the language calls for you that can propagate a PropagateError: toString, valueOf, [Symbol.toPrimitive], toJSON, toLocaleString, [Symbol.iterator], [Symbol.asyncIterator], an iterator's next / return / throw, [Symbol.dispose], [Symbol.asyncDispose], then, [Symbol.hasInstance] and the other well-known symbols, a Proxy handler's traps, an object literal's getters and setters, handleEvent and Node's [inspect.custom]. Reported at the method, in a class or an object literal at any depth, also where the member names a function (toString = format). On by default, as an error.
Why: the propagation rules follow calls, and no call in the source names these methods: `${price}` runs price.toString(), JSON.stringify(order) runs toJSON on everything it holds, a < b runs valueOf, for (const row of table) runs [Symbol.iterator] and every next(), await job runs job.then, using lock = ... runs [Symbol.dispose] when the block ends. A failure there reaches the code around with no caller told, and the places that run them are everywhere and mostly untyped (String(x), a template, console.log, a library, a value typed by an interface it happens to fit). The definition is one place, and a toString that fails is a design error on its own: make the implicit method total, and put what can fail in a method the caller names. Guide: Errors.
✕ Rejectedimplicit-propagation
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Unpriced extends PropagateError {}
export class Price {
readonly cents: number;
constructor(cents: number) {
this.cents = cents;
}
toString(): string {
if (this.cents < 0) throw new Unpriced("negative");
return `${(this.cents / 100).toFixed(2)} EUR`;
}
}✅ Accepted: the failure is a Result of a method the caller names, and toString is total
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
export type PriceError = Enum<{ Negative: { cents: number } }>;
export const PriceError = constructors<PriceError>();
export class Price {
readonly cents: number;
constructor(cents: number) {
this.cents = cents;
}
format(): Result<string, PriceError> {
if (this.cents < 0) return err(PriceError.Negative({ cents: this.cents }));
return `${(this.cents / 100).toFixed(2)} EUR`;
}
toString(): string {
const text = this.format();
return isErr(text) ? "(no price)" : text;
}
}
export function label(price: Price): string {
const text = price.format();
if (!isErr(text)) return text;
return match(text.error, { Negative: ({ cents }) => `refund of ${-cents / 100} EUR` });
}ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
export type PriceError = Enum<{ Negative: { cents: number } }>;
export const PriceError = constructors<PriceError>();
export class Price {
readonly cents: number;
constructor(cents: number) {
this.cents = cents;
}
format(): Result<string, PriceError> {
if (this.cents < 0) return err(({ tag: "Negative", cents: this.cents } satisfies PriceError as PriceError));
return `${(this.cents / 100).toFixed(2)} EUR`;
}
toString(): string {
const text = this.format();
return isErr(text) ? "(no price)" : text;
}
}
export function label(price: Price): string {
const text = price.format();
if (!isErr(text)) return text;
{ const __ms1 = text.error; let __mv1: string; switch (__ms1.tag) { case "Negative": { const { cents } = __ms1; __mv1 = `refund of ${-cents / 100} EUR`; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }
}An iteration that may fail is legitimate (a generator that reads a file), but for...of can't tell anyone: offer it as a named generator method, whose callers are followed where they step it, or yield values that say what failed.
✕ Rejectedimplicit-propagation
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class Truncated extends PropagateError {}
export class Log {
readonly lines: readonly string[];
constructor(lines: readonly string[]) {
this.lines = lines;
}
*[Symbol.iterator](): Generator<string> {
for (const line of this.lines) {
if (!line.endsWith(";")) throw new Truncated(line);
yield line;
}
}
}✅ Accepted: the caller names entries(), so what it can propagate reaches the caller's caller
✓ Accepted0 diagnostics
ts
import { PropagateError } from "@evobug_com/safe-ts-std/result";
export class Truncated extends PropagateError {}
export class Log {
readonly lines: readonly string[];
constructor(lines: readonly string[]) {
this.lines = lines;
}
*entries(): Generator<string> {
for (const line of this.lines) {
if (!line.endsWith(";")) throw new Truncated(line);
yield line;
}
}
}
export function count(log: Log): number {
let n = 0;
for (const _line of log.entries()) n += 1;
return n;
}Who calls what
Each protocol of the language, who calls its method, and whether the checker follows that call (it follows a call it can tie to one function: see uncaught-propagation).
| Protocol | Called by | Followed | Here |
|---|---|---|---|
toString, valueOf, [Symbol.toPrimitive] | templates, String(x), +x, x + "", x < y, join, property keys | no | refused |
toJSON | JSON.stringify, at any depth | no | refused |
toLocaleString | toLocaleString() of a list, Intl formatting | no | refused |
[Symbol.iterator], an iterator's next / return / throw | for...of, spread, destructuring, Array.from, new Map(x), Promise.all(x), yield* | a generator method called by name (for (const x of t.rows())) is; the protocol isn't | refused |
[Symbol.asyncIterator] | for await, Array.fromAsync | no | refused |
[Symbol.dispose], [Symbol.asyncDispose] | using, await using, when the block ends | no | refused |
then of a non-promise | await, Promise.resolve(x), returning it from an async function | no | refused |
[Symbol.hasInstance] | instanceof | no | refused |
species, toStringTag, isConcatSpreadable, match, matchAll, replace, search, split, unscopables | the built-ins that look them up | no | refused |
a Proxy handler's traps | every operation on the proxy | no | refused (a handler written in new Proxy(...) or a const it names) |
| a class's getter and setter | x.label, x.label = v, const { label } = x | yes, where the receiver's class is written down | followed |
| an object literal's getter and setter | reads, spread, JSON.stringify, Object.entries, structuredClone | no | refused |
handleEvent, [inspect.custom] | the DOM's listeners, console.log / util.inspect | no | refused |
a tagged template (tag`...`) | the template | yes | followed |
a decorator (@sealed, @logged("x")) | the class definition | yes | followed |
| a class's static block and static field initializers | loading the module (the class definition) | yes | followed |
| a class's instance field initializers | new C() | yes, as part of the constructor | followed |
Following was rejected for the conversions: their callers are every template, String(x), comparison and library that ever sees the value, most of them untyped, so following can never be complete, and on an adopter there was nothing to follow (no class or object defined any of these methods; four Proxy traps in tests and one object literal getter, none of which can fail).
What the rule can't see
- A method added another way: assigned later (
price.toString = format), put on a prototype (Price.prototype.toString = ...), defined withObject.defineProperty, or made by a library (a class extending a package's class inherits its implicit methods unseen). - A
Proxywhose handler isn't written there: a handler passed in, returned by a function, or built from parts. - A failure that isn't a
PropagateError: a built-in that throws inside the method is reported where it is called (unhandled-failure), athrowof another error byno-throw. - A call the method makes through
thisto a method a subclass overrides: the base's method is followed, not the override's. - The call sites: nothing is reported where the method runs. A class whose
toStringis total but that inherits a failing one from a base in another program isn't seen either.