TypeScript Compiler API
ts.WatchCompilerHost
Defines the watch host boundary: compiler creation, file reads, watch registrations, timers and callbacks. Root-file and configuration-file hosts extend this interface. Watch registration returns a closeable handle. A controlled host must deliver accurate events, keep file identities consistent and cancel scheduled work. This example tests registration and closure, not operating-system watcher reliability. 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
interface ts.WatchCompilerHost<T extends ts.BuilderProgram> extends ts.ProgramHost<T>, ts.WatchHostWorking 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 = 1;");const c = controlledSystem();const host = ts.createWatchCompilerHost([path.resolve("main.ts")], { noEmit: true, types: [] }, c.system, ts.createSemanticDiagnosticsBuilderProgram, () => {}, () => {});const watcher = ts.createWatchProgram(host);assert(c.active() > 0);watcher.close();assert.equal(c.active(), 0);c.clear();console.log("watch registrations closed");
// Run: node example.cjs// Expected stdout: watch registrations closed