TScert verifies type safety for strict TypeScript functions against a formal operational calculus in Lean 4. Lean synthesizes untrusted type, effect, and loop-invariant proposals, then validates each code unit in a single forward pass proved sound in Lean.
tscert-launch.mp4
TypeScript admits unsound patterns (any, type assertions, unchecked indexed
reads, covariant mutable arrays, bivariant methods) where tsc-accepted code
produces undefined at a string type or throws TypeError at runtime. For
every accepted unit, TScert's Lean soundness theorem guarantees that its
calculus translation never gets stuck and never returns a value outside its
declared type under conforming inputs.
TypeScript accepts stale under strict (noUncheckedIndexedAccess off);
TScert refuses it because xs.pop() invalidates the length bound:
export function stale(xs: string[]): string {
const n = xs.length;
xs.pop();
return xs[n - 1]; // undefined at run time
}Abridged output:
"status": "unverified"
reject: source expression obligation at [219,228): evidence search failed: no derivation of its return-contract claim
obligation: its operands' claims are undefined | string; its claim is string
Changing the return annotation to string | undefined verifies. Conversely,
noUncheckedIndexedAccess rejects total, whereas TScert proves i < xs.length
keeps every read in bounds:
export function total(xs: string[]): number {
let n = 0;
for (let i = 0; i < xs.length; i++) n += xs[i].length;
return n;
}TypeScript also accepts covariant mutable arrays under every compiler flag; TScert refuses the aliasing assignment because writable arrays are invariant:
export function shout(xs: string[]): string[] {
const ys: (string | number)[] = xs;
ys.push(1);
return xs.map((x) => x.toUpperCase()); // throws TypeError under Node
}Pipeline cases:
invalid-array-last-after-pop.ts,
array-last-element.ts,
array-index-loop.ts, and
invalid-array-covariance.ts.
- A front end on TypeScript's compiler API parses strict TypeScript into a Fragment AST and collects referenced declarations.
- Lean builds execution plans, synthesizes untrusted proposals (expression and binding types, footprints, generic instantiations, loop-head invariants), and lowers each unit to a calculus control-flow graph.
- A single forward pass (
SinglePass.lean) checks every node and edge against proved rules (SINGLE-PASS.md, stages).
The headline theorem
SourceClaimJson.PassUnit.mode8Accepted_sound
proves that every function in an accepted unit is safe (PassUnitSafe →
SafeGraph / Safe): for inputs and a heap satisfying their declared types
and kinded-world invariants, the lowered calculus program never gets stuck and
any normal return satisfies its return contract. Divergence and exceptions in
the exception family are permitted.
Trusted boundaries outside the Lean proof:
- faithfulness of TypeScript parsing and AST-to-calculus lowering;
- agreement of reviewed library/host overlays and the operational calculus with JavaScript;
- conformance of callers and external imports to their declared contracts;
- the Lean 4 kernel, compiler, and stage-store replay integrity.
Exact statement and complete TCB: What "verified" means and Trust surface.
- Core calculus & operational TCB: docs/SEMANTICS.md
(full
Tm/CallCode/Step/Raisesgrammar, kindedWorldT,HeapOk,Safe/SafeGraph,Promise/PromiseWith, and exact TCB module list). - Headline soundness theorem:
PassUnitRun.lean(mode8Accepted_sound,Mode8Accepted.{main,slice,freshModule},replayedSliceVerdict_sound) and docs/SINGLE-PASS-THEOREM.md. - Type system & single pass: docs/POSITIVE-TYPES.md, docs/RECURSIVE-EQUATIONS.md, docs/FOOTPRINTS.md, docs/ARITHMETIC.md, and docs/SINGLE-PASS.md.
TScert checks strict TypeScript covering const/let, destructuring,
control flow (if, switch, loops including for...of/for...in, labels,
throw, try/catch/finally), primitives and literal types, arithmetic and
comparisons, objects with spreads and index signatures, arrays, tuples, typed
arrays, enums, classes, closures, optional/default/rest parameters, generic
functions/classes, async/await over native promises, and reviewed library
contracts. any is refused, and e as T / e! generate checked obligations.
Excluded constructs are listed in
SOURCE-FRAGMENT.md.
At v1.1.0, TScert verifies two nontrivial TypeScript codebases:
- TScert's own front end (
tools/, ~47,000 lines of TypeScript, proved mode): all 46,638 lines of verifiable code across 52 modules (1,155 top-level functions; 25,656 of 25,656 verifiable statements) pass per-function verification against their declared module interfaces (shallow verification, CENSUS.md; census offf653a638, 2026-12-03). The remaining 504 lines (4 files; 19 of 1,174 top-level functions) are minimal, hand-audited host-boundary wrappers (trusted externs). CI requires 100% coverage of verifiable code onmain. Current status lives in WORK.md. - Pretext (~9,700 lines of
TypeScript, experimental paper-rules mode): all 12 library modules (267
code units) of this text-layout library are accepted with every array read
(
a[i]) left bare—proved in bounds statically, without runtime bounds checks (chenglou/pretext#538, mirrored indev/paper-rules/pretext/). Explicit API and domain runtime checks remain and are audited separately (guard audit). Verifying Pretext uncovered two upstream out-of-bounds bugs (reproductions). Paper-rules mode is an experimental checker for code with callbacks and module-private state: its new rules are proved sound in a standalone Lean model, not yet folded into TScert's main soundness proof, so its verdicts are reported separately (details).
@cristianoc/tscert ships native binaries for Linux x64 (with Bubblewrap /
unprivileged user namespaces) and Apple Silicon macOS (with the Node version
pinned in .nvmrc):
npm install --save-dev @cristianoc/tscert
npx tscert src/example.ts --project . --contracts
npx tscert --batch src/first.ts src/second.ts --project .Commit-pinned previews are also published per pull request via pkg.pr.new and
as GitHub Actions tarballs (tscert-linux-x64, tscert-darwin-arm64).
The ordinary CLI outputs JSON verdicts (one JSON line per file in --batch
mode) and exits 0 only when all inputs verify. Projects writing ordinary effect
annotations (Modifies, Fresh, SafeInt, Dense) add
"@cristianoc/tscert/effects" to compilerOptions.types.
For codebases using bounded effect variables (AtMost<P>), confined module
state (Effects<P, T>, Join<E, P>, "@state"), and @callbackRequires
(including Pretext's line walkers across all 12 source modules), @cristianoc/tscert
includes the paper-rules CLI (tscert --paper / tscert-paper) and type
declarations ("@cristianoc/tscert/paper"):
npx tscert --paper src/file.ts --project .
npx tscert-paper --batch src/a.ts src/b.ts --project . --jsonPaper-mode verdicts stay separate from ordinary tscert's proved
mode8Accepted_sound verdict and name the prototype's paperKeptFactsSurvive
axiom. Full CLI and --contracts / --contracts=ts output documentation lives
in REQUEST-PROTOCOL.md and
AGENT-GUIDE.md.
Prerequisites: Node (.nvmrc) and Lean
(lean/lean-toolchain).
make setup # Node dependencies
make build # Lean verifier and stage runner
make check FILE=pipeline/examples/array-last-element.ts # verify one file (CONTRACTS=1 or CONTRACTS=ts)
make test # make test-checks && make test-cases
make help # all targets- Architecture & TCB: DESIGN.md (documentation map), LANGUAGES.md, TRUST-REDUCTION.md.
- Theory: SEMANTICS.md, POSITIVE-TYPES.md, RECURSIVE-EQUATIONS.md, EXPRESSION-TYPING.md, STATE-AND-REFINEMENT.md, FOOTPRINTS.md, ARITHMETIC.md, SINGLE-PASS.md.
- Source & contracts: SOURCE-MODEL.md, SOURCE-ELABORATION.md, SOURCE-CONTRACTS.md, SOURCE-FRAGMENT.md, ANNOTATIONS.md, AGENT-GUIDE.md.
- Benchmarks & development: CENSUS.md, AGENTS.md, WORK.md, tests/README.md, REFERENCES.md.
TScert builds on Appel and McAllester's indexed model, Ahmed's semantics of types for mutable state, Wright and Felleisen's syntactic type soundness, Reynolds's parametricity, Abadi and Cardelli's theory of objects, semantic subtyping, JSCert, the ECMAScript specification, and Lean 4. See REFERENCES.md.
Licensed under the NON-AI MIT License: MIT terms excluding use in training or improving machine-learning systems and training datasets, including derivatives. Text and data mining rights are reserved under Article 4(3) of Directive (EU) 2019/790 (ai.txt).
