Skip to content

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.

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.

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).

ProtocolCalled byFollowedHere
toString, valueOf, [Symbol.toPrimitive]templates, String(x), +x, x + "", x < y, join, property keysnorefused
toJSONJSON.stringify, at any depthnorefused
toLocaleStringtoLocaleString() of a list, Intl formattingnorefused
[Symbol.iterator], an iterator's next / return / throwfor...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'trefused
[Symbol.asyncIterator]for await, Array.fromAsyncnorefused
[Symbol.dispose], [Symbol.asyncDispose]using, await using, when the block endsnorefused
then of a non-promiseawait, Promise.resolve(x), returning it from an async functionnorefused
[Symbol.hasInstance]instanceofnorefused
species, toStringTag, isConcatSpreadable, match, matchAll, replace, search, split, unscopablesthe built-ins that look them upnorefused
a Proxy handler's trapsevery operation on the proxynorefused (a handler written in new Proxy(...) or a const it names)
a class's getter and setterx.label, x.label = v, const { label } = xyes, where the receiver's class is written downfollowed
an object literal's getter and setterreads, spread, JSON.stringify, Object.entries, structuredClonenorefused
handleEvent, [inspect.custom]the DOM's listeners, console.log / util.inspectnorefused
a tagged template (tag`...`)the templateyesfollowed
a decorator (@sealed, @logged("x"))the class definitionyesfollowed
a class's static block and static field initializersloading the module (the class definition)yesfollowed
a class's instance field initializersnew C()yes, as part of the constructorfollowed

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 with Object.defineProperty, or made by a library (a class extending a package's class inherits its implicit methods unseen).
  • A Proxy whose 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), a throw of another error by no-throw.
  • A call the method makes through this to 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 toString is total but that inherits a failing one from a base in another program isn't seen either.

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