TypeScript Compiler API
WatchCompilerHost.afterProgramCreate()
A hook invoked after watch program creation, including rebuilds. Preserve the original callback if you still want default diagnostic reporting and emission. Use the supplied current builder rather than stale state. Avoid starting recursive watch work in the callback. The fixture records two program generations using controlled notifications. Baseline: TypeScript 5.8.3, Node.js. Run as a local tooling script in a disposable directory; examples create files. Not a ByteMind CodeRunner example.
Formal Syntax & Structure
typescript syntaxtypescript
host.afterProgramCreate?: (program: T) => voidParameters & Attributes
| Name | Type | Description |
|---|---|---|
| program | T extends ts.BuilderProgram | Current builder provided to the callback. |
Working Example
Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function put(name, text) { fs.mkdirSync(path.dirname(name), { recursive: true }); fs.writeFileSync(name, text);}// Controlled notifications and timers; actual file reads/writes use ts.sys.function controlledSystem() { const watchers = new Map(), directories = new Set(), timers = new Map(); let serial = 0; const system = { ...ts.sys, watchFile(name, cb) { const key = path.resolve(name); const list = watchers.get(key) ?? new Set(); watchers.set(key, list); list.add(cb); return { close() { list.delete(cb); } }; }, watchDirectory(name, cb) { const entry = { name, cb }; directories.add(entry); return { close() { directories.delete(entry); } }; }, setTimeout(cb, ms, ...args) { const id = ++serial; timers.set(id, () => cb(...args)); return id; }, clearTimeout(id) { timers.delete(id); } }; function change(name, text) { const key = path.resolve(name); const callbacks = [...(watchers.get(key) ?? [])]; assert(callbacks.length > 0, "file must be watched"); put(key, text); // Force a distinct modification time even on coarse filesystems. const later = new Date(Date.now() + 2000); fs.utimesSync(key, later, later); for (const cb of callbacks) cb(key, ts.FileWatcherEventKind.Changed); } function drain() { for (let count = 0; timers.size; count++) { assert(count < 50, "timer loop did not settle"); const [id, cb] = timers.entries().next().value; timers.delete(id); cb(); } } return { system, change, drain, active: () => [...watchers.values()].reduce((n, s) => n+s.size, 0)+directories.size, clear: () => { watchers.clear(); directories.clear(); timers.clear(); } };}put("main.ts", 'export const n: number = "bad";');const root = path.resolve("main.ts"), c = controlledSystem(), seen = [];const host = ts.createWatchCompilerHost([root], { strict: true, noEmit: true, types: [] }, c.system, ts.createSemanticDiagnosticsBuilderProgram, () => {}, () => {});const previous = host.afterProgramCreate;host.afterProgramCreate = b => { previous?.(b); seen.push(b.getSemanticDiagnostics().map(d=>d.code));};const watcher = ts.createWatchProgram(host);try { c.change(root, "export const n: number = 7;"); c.drain(); assert.deepEqual(seen, [[2322], []]); console.log("2322 -> clean");} finally { watcher.close(); c.clear(); }
// Run: node example.cjs// Expected stdout: 2322 -> clean