Skip to content

What the transforms guarantee ​

SAFE-TS's build-time rewrites (q(), match(), parseJson<T>(), loops, and @inline) only transform code when behavior stays equivalent; otherwise they leave your code untouched. Run safe-ts explain <file> to inspect every rewrite applied in a file and why any candidate was skipped.

In short

  • Exact line numbers: rewrites stay on their original lines so stack traces, breakpoints, and coverage match your source.
  • Strict evaluation order & determinism: expressions evaluate once in written order and produce byte-identical builds for stable bundle hashing and V8 code caching.
  • Type-checked output: safe-ts emit writes transformed TypeScript so tsc can verify every generated validator and rewrite.
  • Same module resolution as safe-ts check: honors relative paths, tsconfig.json paths, workspaces, and Vite aliases.

Kept for every rewrite ​

  • Line numbers. Rewritten code stays on the lines it came from, so stack traces, breakpoints and coverage point at what you wrote. Source maps map each line to itself.
  • Evaluation order. Expressions run in the order you wrote them, each exactly once.
  • Determinism. The same input gives byte-identical output, so bundles hash the same and code caches survive rebuilds (v8.dev). A test checks it. The Node loader and the Bun plugin rely on it too: they cache their output on disk and reuse it while the file, and every file the rewrite read for it, are unchanged (Running TypeScript in Node).
  • Types. safe-ts emit writes the rewritten program so tsc can check it (and so Node or Bun can run it without the loader); generated code keeps the types (satisfies T as T for enum constructors, for example). The validators of named types are written once, next to the file declaring the type (types.ts.safe-ts-json.ts), and checked with the rest.
  • Imports, as the checker sees them. A rewrite that reads another file (the type a validator is built from, an @inline function, an enum table) finds it the way safe-ts check does: relative paths, tsconfig.json paths aliases, workspace packages, and in the Vite plugin Vite's string aliases too (How imports are followed). So a validate<T> that check accepts, the build accepts, and the other way round.
  • Shared modules. A shared validator module's content depends only on its name (type, type arguments, options) and the type declarations, so every file that imports it gets the same module. The bundler plugin, the Node loader and the Bun plugin treat it like a file: it is rebuilt (or its cache entry dropped) when a declaration it was built from changes.

Per rewrite ​

RewriteKeptChanged
q(x) → early returnThe value and the Err returned–
q(x, f) → early returnThe value, and f(error) returned in an Errf is evaluated only for an error
match(...) → switchWhich handler runs (a missing value goes to undefined, else _), its result, exhaustiveness– (on .tag for an enum, on the value for a union of strings; a value whose type the build can't find stays a call in TypeScript)
is/variant → tag checkThe answer (a missing value is no variant); the value is evaluated once–
parseJson<T> → validatorAccepts and rejects exactly what T describes, with the same errorsUnknown fields are dropped (or reported, with unknownKeys: "error"); key order is the declared one; a named type's validator is imported from a module shared by every file using it (One validator per type)
Enum constructors → literalsThe valueKey order is the declared one when the values are plain
Enum methods → calls of the tableEvaluation order: the value, then the arguments; x?.m() evaluates x once and gives undefined for a missing valueerror.describe() becomes UploadError.describe(error); needed to run, so legacy files get it too (Methods on an enum)
Array callbacks → loopsResults, holes skipped, length read once, exceptionsCallbacks before the last step run element by element (only rewritten when they have no side effects)
Typed-array for...of → index loopValues and order, break/continue, a new binding per step, length read each stepA detached buffer ends the loop instead of throwing
@inline → bodyArguments evaluated once, in orderThe call frame disappears from stack traces
@loop → loopResults; arguments evaluated before parameters changeRecursive frames disappear from stack traces
@json → JSON.parseThe value (only JSON-exact data is accepted)–

match(...), is and variant are rewritten only for speed: imported by name, they work without the transform too (in plain bun, say). q(...), parseJson<T>, validate<T>, arbitrary<T>() and prelude names don't: they need it (What needs no transform).

In development the transform can also add dev checks: they pass every value through unchanged and throw only where a value breaks its type. Production builds don't contain them.

Legacy and excluded files ​

A file marked // @safe-ts legacy (and listed under "legacyFiles" in package.json: an unlisted marker is ignored, here as in the checker) gets only the rewrites it needs to run: prelude imports, q(...), match(...), enum methods called on their values, and the validators of parseJson<T> / validate<T>. The rest of the table (enum constructors and is/variant, which stay runtime calls, array callbacks and typed-array loops, @inline, @loop, @json) and the dev checks are left out, so the shipped code of a file is what you wrote until you migrate it. safe-ts explain marks those sites "not applied, the file is marked legacy".

safeTs({ exclude: [...] }) leaves files out entirely: no check, no rewrite. Such a file must not need the transform (Adopting SAFE-TS).

What none of them keep: Function.prototype.toString() of a rewritten function shows the rewritten code (v8.dev), and code that inspects its own stack trace sees the frames that remain.

How it is checked ​

Every rewrite has exact input/output tests, and the std test suite runs rewritten code against the expected results, including the edge cases (holes, arrays changed while iterated, arguments with side effects, deep recursion). Each docs benchmark also checks that both versions return the same result before timing them.

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