Skip to content

Dev checks ​

Static rules catch what can be proven from source, and every rule page documents its blind spots. For the narrow gaps where runtime data can violate its declared type, the build transform inserts Rust-style debug assertions in development—stopping immediately with the exact file, line, and docs link—while stripping them completely from production builds.

In short

  • Automatic in dev, stripped in prod: enabled in Vite dev/Vitest, the Node loader, and Bun preload; omitted from vite build and Bun.build (override with safeTs({ devChecks }) or SAFE_TS_DEV_CHECKS=1|0).
  • What gets checked: q(...) handed a Promise or unrecognized Err, match(...) receiving an unhandled tag or undefined, and .sort() comparators returning NaN or non-numbers.
  • Clear failure signal: throws SafeTsDevCheck with the exact source location.
  • Zero production footprint: production bundles are byte-for-byte identical to builds with dev checks disabled.

When they are on ​

WhereDev checks
Vite dev server (vite, vite dev, and tools that run Vite in serve mode, such as Vitest)on
vite buildoff
Rollup or Rolldown without Viteoff
Node loader (--import @evobug_com/safe-ts-transform/register)on, unless NODE_ENV=production
Bun preload (@evobug_com/safe-ts-transform/bun)on, unless NODE_ENV=production
Bun.build with safeTsBun()off
safe-ts emit, safe-ts explainoff

The plugin decides from Vite's command: "serve" turns them on, "build" off. Set it yourself with safeTs({ devChecks: true }) or safeTs({ devChecks: false }). For the Node loader and the Bun preload, SAFE_TS_DEV_CHECKS=1 or =0 (also true / false) wins over NODE_ENV. The transform(file, code, { devChecks: true }) function the plugin uses leaves them out unless asked.

What is checked ​

q(...) given a Promise ​

q takes a Result, and to TypeScript a Promise<Result<T, E>> is one too (its T is the promise). std's types refuse a promise there: q(saveUser(user)) doesn't compile ("this is a promise: await it, then pass the value"), and promise-as-value reports the ways around the types that it can see, such as a generic helper of yours handed an async callback. What is left is a value neither of them can tell is a promise, such as a package's function its types leave any:

ts
import { q, type Result } from "@evobug_com/safe-ts-std/result";
import { users } from "untyped-client"; // no types: `users.save` is `any`, and returns a promise

export async function rename(user: User, name: string): Promise<Result<void, SaveError>> {
  q(users.save({ ...user, name })); // should be q(await users.save(...))
  return undefined;
}

Without the check, the Promise is the "value", rename reports success, and the Err the Promise resolves to is never returned. In development it stops at once:

SafeTsDevCheck: safe-ts dev check failed at src/users.ts:10:3: q() got a Promise, not a
Result: add `await`, q(await ...). Without it the Promise passes through as the value and an
Err inside it is never returned
  See https://safe-ts.evobug-pages.com/rules/promise-as-value

The same check stops an Err that q can't recognize. q tests instanceof Err against the Err class the file imports. When a bundle loads @evobug_com/safe-ts-std twice (two versions in node_modules, or one module reached through two paths), an Err made by the other copy is a different class, and q passes it on as a value. The check reports any object whose class is named Err, has an error field, and isn't the imported Err.

match given a variant it has no handler for ​

A match without _ handles every variant of its type. A value that didn't come from that type can still reach it: an as cast of JSON.parse output, a type guard that lies, or a server that started sending a variant this build doesn't know about. Without the check the match throws from unreachable with the value, without naming the tags it handles. The check names the match, the tag it got and the tags it handles:

SafeTsDevCheck: safe-ts dev check failed at src/payments.ts:7:10: match got the variant
"Crypto", which has no handler here (handled: Card, Invoice). ...
  See https://safe-ts.evobug-pages.com/rules/no-unchecked-cast

On a union of strings, the check names the string it got (match got "close", which has no handler here (handled: open, urls)). A value that is neither a string nor has a string tag (undefined, a plain object) is reported the same way, except undefined when the match has an undefined handler (a match on an Option). A match with _ isn't checked: _ handles the other variants (and a missing value) on purpose. Every match whose handlers are an object literal in the call is checked, also the ones the transform leaves as calls.

Comparators that don't return a number ​

The sort-comparator rule reports comparators it can see are wrong. It can't see (a, b) => a.price - b.price returning NaN because some item has no price: the sort then finishes without an error, in an order that depends on the engine. In development, each comparator passed to .sort(...) or .toSorted(...) is checked on every call: a result that isn't a number, or is NaN, stops with the two values compared:

SafeTsDevCheck: safe-ts dev check failed at src/cart.ts:4:26: the comparator returned NaN
for {"name":"a","price":2} and {"name":"b"}, so the order is up to the engine. ...
  See https://safe-ts.evobug-pages.com/rules/sort-comparator

What the transform adds ​

Each check wraps one value, on the line it is on, so line numbers stay what you wrote:

ts
// as written
q(saveUser({ ...user, name }));
// in development (before the q rewrite)
q(__safeTsDevQ(saveUser({ ...user, name }), "src/users.ts:10:3"));

The checks themselves are small functions appended after the file's last line, only the ones the file uses. In production none of this is emitted: npm run test:vite checks that the demo's vite build output contains none of it, and that the dev server serves it.

Cost ​

Only in development:

  • q(...): one function call and one or two typeof tests per evaluation.
  • match: one function call, an array of the handled tags, and a search of it (as many entries as the variants).
  • A comparator: one extra call and a typeof test per comparison.
  • A few hundred bytes of helpers in each file that uses one of them.

In production, nothing.

That is small, but not nothing in a hot path: hashing a large state object ran 1.10–1.32× slower with the checks, and on par without. Benchmarks run through the Node loader or Vitest have them on, so turn them off there: SAFE_TS_DEV_CHECKS=0 node --import @evobug_com/safe-ts-transform/register bench.ts, or safeTs({ devChecks: false }) in Vitest's config (Performance myths).

What isn't checked, and why ​

  • Parameters changed without Mut<T> (no-param-mutation can't see a change made through a copy of the reference). Freezing the argument would freeze the caller's object for good, so the caller's own later, correct changes would throw in development and work in production. A read-only Proxy instead changes the object's identity (===, Map keys, #private fields). Both make correct code behave differently in development, so neither is done.
  • Objects changing shape after construction (stable-shape). JavaScript can't see an object's hidden class without V8's debugging flags, and comparing key lists at every use would cost more than the slowdown it looks for.
  • A type guard that lies in a file the rules don't check (a // @safe-ts std module, a .d.ts): in checked code a hand-written guard is reported (no-type-predicate). Checking its claim needs a validator for the type, and only JSON types have one (validate<T>): use that instead of the guard.
  • Comparators that are inconsistent without returning NaN (compare(a, b) and compare(b, a) both positive). Checking that doubles the comparisons.
  • isErr(...) and instanceof Err written by hand, and <Match> in Vue templates: only q(...) and match(...) calls in scripts are checked.

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