Skip to content

API changes ​

safe-ts api-diff compares your package's public TypeScript surface against any git ref and reports breaking changes—similar to Rust's cargo-semver-checks.

In short

  • Compare against any git ref: run safe-ts api-diff v1.4.0 in a package directory to diff against package.json exports (or pass explicit entry files).
  • CI-ready exit codes: exits 1 on breaking changes and supports --json for structured reporting.
  • SAFE-TS-aware: tracks Result error types, escaping PropagateError classes, Enum variants, and Mut<T> signatures.
  • Explicit uncertainty: operates syntactically without running tsc, reporting "can't classify" instead of guessing when type relationships aren't syntactically decidable.

Running it ​

sh
safe-ts api-diff v1.0.0                    # entries from package.json
safe-ts api-diff origin/main src/index.ts  # these files instead
safe-ts api-diff HEAD~1 --json

The ref is anything git understands: a tag, a branch, a commit. The old version is checked out into a temporary git worktree (without running hooks) and removed afterwards. The new version is what is on disk, uncommitted changes included. In CI, the clone needs the ref (fetch-depth: 0 on most checkouts).

Exit codes: 0 when nothing is breaking (compatible and "can't classify" changes don't fail), 1 when something is, 2 when the ref or the entries can't be found.

Say v1.0.0 had this module:

ts
export type Options = { retries: number; verbose?: boolean };
export type Order = Enum<{ Open: { items: number }; Shipped: { tracking: string } }>;

export function loadOrder(id: string, options: Options): Result<Order, "not-found"> {
  return err("not-found");
}

and the working tree has this one:

ts
export type Options = { retries: number; timeout: number; verbose?: boolean };
export type Order = Enum<{ Open: { items: number }; Shipped: { tracking: string }; Cancelled: { reason: string } }>;

function connect(): void {
  throw new Offline("no network"); // Offline extends PropagateError
}

export function loadOrder(id: string, options: Options): Result<Order, "not-found" | "forbidden"> {
  connect();
  return err("not-found");
}

export function cancelOrder(id: string): Result<void, "not-found"> {
  return err("not-found");
}

The output, grouped by export:

text
API changes since v1.0.0 (5fe871a6f5), entries: .

  Options      type  src/index.ts:6 (taken from callers)
    breaking       timeout: new required key
  Order        type  src/index.ts:7 (returned to callers)
    breaking       Cancelled: new variant: `match` calls without `_` miss it
  loadOrder    function  src/index.ts:13
    breaking       (return) error: new error kind `"forbidden"`
    breaking       can now propagate `Offline` (a PropagateError) to its callers
  cancelOrder  function added  src/index.ts:18
    compatible     added

✗ 4 breaking changes, 1 compatible, 0 can't classify

The Offline line comes from the checker's whole-program pass: loadOrder didn't change its signature, but a function it calls now throws a PropagateError that nothing catches on the way, so loadOrder's callers get it.

With --json, the report has breaking, compatible and unclassified counts, and for each entry its exports with name, kind, status (added, removed, changed), location, usage (for types) and changes (level, at, message).

What counts as breaking ​

Whether a change breaks callers depends on which way the value goes. Callers read return types, constants, class members and the fields of what they get back. They write parameters and the fields of what they pass in.

ChangeIn what callers readIn what callers write
Export removedbreakingbreaking
Export addedcompatiblecompatible
Union member added (string → string | undefined)breakingcompatible
Union member removedcompatiblebreaking
Required key addedcompatiblebreaking
Optional key addedcompatiblecompatible
Key removedbreakingbreaking (object literals that still set it fail the excess-property check)
Key made optionalbreakingcompatible
Key made requiredcompatiblebreaking
readonly added (key or array)breakingcompatible
New error kind in Result<T, E>breakingcompatible
Error kind removedcompatiblebreaking
Enum variant addedbreaking (match without _ misses it)compatible
Enum variant removedbreaking (a match arm for it no longer compiles)breaking

For functions:

Change
New required parameterbreaking
New optional parametercompatible
Parameter removedbreaking (calls that pass it no longer compile)
Parameter made required / optionalbreaking / compatible
A new PropagateError kind can escape (also from a class method or constructor)breaking
A kind that was thrown now comes back as a rejected promise, or the other way roundbreaking (a try without await catches only thrown errors, .catch(...) only rejections)
A PropagateError kind no longer escapescompatible
T → Mut<T> parameterbreaking: callers must now lend the value with mut(...), and the function may change it
Mut<T> → T parametercompatible: callers' mut(...) still compiles (unneeded-mut reports it)
Mut<A> → Mut<B>breaking, whichever is wider: Mut is checked in both directions
Sync → async, return type not writtenbreaking

Parameters that are callbacks turn this around: a callback's parameters are values the library writes and the callback reads. (value: number) => void becoming (value: number | string) => void is breaking, because callers' callbacks may now get a string. A new parameter passed to callbacks is compatible, since they can ignore it.

Removing an error kind is compatible when the error is a union of values. When it is an Enum, it is breaking: match on the error with an arm for the removed variant no longer compiles (TypeScript rejects the extra handler key).

Which way a type goes ​

An exported type is used through the exports that mention it. Options above is only taken by loadOrder, so it is input: a new required key breaks callers. Order is only returned, so it is output: a new variant breaks callers who match on it. A type used both ways, or not used by any other export (callers may build it and read it), counts both ways: a change that breaks either side is breaking. The output says which one it is, next to the type.

Types the module doesn't export can't be named by callers, so they are expanded where they are used. A change to such an alias shows up on every export that uses it:

ts
type Id = string; // not exported; becomes `string | number`

export function search(query: { id: Id }): Id[] {
  return [];
}
text
  search  function  src/index.ts:3
    breaking       (return)[]: can now also be `number`
    compatible     query.id: now also accepts `number`

What it can't see ​

  • It has no type checker. Types are compared as normalised syntax: unions flattened and sorted, aliases expanded, boolean the same as true | false. It understands Option, Result, Enum, Mut, Readonly, Partial, Required, Pick and Omit (with literal keys), arrays, tuples, object types, interfaces (including extends of types it can expand) and function types. Anything else (conditional and mapped types, keyof, typeof, template literal types, indexed access) is compared as text. If it changed, the change is "can't classify".
  • Library types are names to it. Box<T> becoming Box<T | undefined> is "can't classify": it doesn't know how Box uses T. Built-in containers (Promise, Array, Map, Set, Record, ...) are compared element by element, as TypeScript compares them.
  • Types that aren't written down. A function without a return type: only a change between sync and async is noticed. A constant whose type is inferred from a call: if the call changed, the change is "can't classify". A constant initialised with a literal counts as its primitive type ("safe" as string), so a changed value isn't reported. Object literals and arrow functions are read like declared types. Declare the types of what you export.
  • Behaviour. A function that keeps its signature and does something different isn't a type change. Thrown PropagateErrors are the exception, because the checker already tracks them. They are tracked for exported functions and for a class's public methods, static methods and constructor (field initializers included), through the same calls the checker follows. Methods of an exported object literal aren't tracked, and calls the checker can't tie to one function aren't followed (see uncaught-propagation).
  • Overloads are compared as a whole, not signature by signature.
  • Classes: members inherited from a base class aren't merged in, and a changed extends is "can't classify".
  • Renames: a renamed export is a removal plus an addition. Signatures that use a renamed exported type are "can't classify", because the names differ.
  • A narrower return can still break a build. Removing a union member from a return type is compatible under the usual rules, but TypeScript rejects a case against a value that can no longer occur, so callers with such a switch stop compiling. Removing a key from an input type is reported as breaking for the opposite reason: an object literal that sets it fails, though a variable passed in would still compile.
  • Entries: exports with * patterns are skipped, so name those files. An export target that points at build output not in git (./dist/index.js) is replaced by its .ts twin when one exists. Otherwise, name the source files. Subpaths that point at data (./recommended.json) aren't entries. A directory git ignores (a built .release/ or dist/ package) has no old version, so api-diff refuses to run there; run it in the package's source directory.
  • Dependencies come from the working tree's node_modules for both versions, workspace siblings included, so a sibling package's own changes don't show up in this package's diff. A dependency whose version changed since the ref is read as it is installed now.
  • JavaScript entries have no types, so only added and removed exports and parameters count.

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