Use with Svelte 5
Goal: collect and apply types for <script lang="ts"> blocks in .
files. @ts-capture/ instruments those blocks and writes inferred types
back into the correct <script> block. It pairs with @ts-capture/, which
supplies the runtime collector for the Vite/Vitest pipeline.
<script lang="ts">
function format(value) {
return value.toFixed(2);
}
</script><script lang="ts">
function format(value: number): string {
return value.toFixed(2);
}
</script>Install #
npm install --save-dev @ts-capture/svelte @ts-capture/vite @ts-capture/core
Peer dependencies: @ts-capture/ and svelte >=5..
Register the preprocessor and plugin #
import { defineConfig } from "vitest/config";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import tsCapture from "@ts-capture/vite";
import { sveltePreprocessor } from "@ts-capture/svelte";
export default defineConfig({
plugins: [svelte({ preprocess: [sveltePreprocessor()] }), tsCapture()],
});Collect, merge, apply #
TS_CAPTURE_TYPES_DIR=./.ts-capture npm test
npx ts-capture merge ./.ts-capture --out types.json
npx ts-capture apply types.json --dry-run # preview
npx ts-capture apply types.json
Commit first; apply edits your source (review & apply safely).
Route apply back to .svelte files #
. files reach the CLI as synthetic virtual paths
(<file>. / <file>.). Register
sveltePlugin() so the CLI routes those entries back to the owning .
file via the offset-aware applier — without it, the core CLI warns and skips
every synthetic svelte path.
import { sveltePlugin } from "@ts-capture/svelte";
export default { plugins: [sveltePlugin()] };$state runes inside effects — attachPeek #
A Svelte 5 $state proxy can register reactive subscriptions when read inside
an $effect. If observation may happen in an effect context, wrap the state so
ts-capture walks the unwrapped value via Svelte's snapshot():
import { attachPeek } from "@ts-capture/svelte/runes";
let profile = attachPeek($state({ name: "alice", age: 30 }));
$state({. read outside an effect is walked correctly without this
helper — use attachPeek only when reads happen inside an effect.