ts-capture

Turn runtime values into TypeScript types — and check existing types against them. ts-capture watches how your code is called — in tests, in dev, in the browser — and writes the types it saw back into your source. It also flags existing types that the run contradicts.

1. A codebase without types
  1. 1. A codebase without types
  2. 2. Add ts-capture to the build
  3. 3. Use the app
  4. 4. Apply what it observed
  5. 5. Types, written into your source
An order form whose code has no types. The ts-capture plugin is added to the Vite config, two orders are placed in the browser, and ts-capture apply writes four named types and six annotations into the source, taking type coverage from 77% to 100%.

The recording is made by running every step for real; nothing in it is written by hand. See what it wrote into real projects, the results a reviewer should change included, or try it in your browser, on code or on a JSON value.

Install

Then run your tests, and npx ts-capture apply types.json --dry-run to see what it would write. The getting started tutorial takes a first run from install to apply.

Already typed? Run the tests with TS_CAPTURE_OBSERVE_ANNOTATED=1 and npx ts-capture verify lists the types you wrote that the run contradicts. See check the types you wrote.

ts-capture rewrites your source files, runs your getters while observing, and writes what it observed to disk. Commit your work first, and read review and apply safely before pointing it at a real codebase. The packages are 0.x: the API may change between minor versions.