TypeScript Compiler API
ts.createWatchCompilerHost()
Constructs the host for watch compilation using root files or a configuration-file overload. Reporters receive diagnostics and status messages; a builder factory determines the incremental strategy. Host construction is distinct from starting the watcher. This example inspects the root-file host before watch creation. 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
ts.createWatchCompilerHost(rootFiles: string[], options: ts.CompilerOptions, system: ts.System, createProgram?: ts.CreateProgram<T>, reportDiagnostic?: ts.DiagnosticReporter, reportWatchStatus?: ts.WatchStatusReporter, projectReferences?: readonly ts.ProjectReference[], watchOptions?: ts.WatchOptions): ts.WatchCompilerHostOfFilesAndCompilerOptions<T>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| rootFiles | string[] | Root source files. |
| options | ts.CompilerOptions | Compiler configuration. |
| system | ts.System | Filesystem and watch services. |
| createProgram | ts.CreateProgram<T> | Builder factory. |
| reportDiagnostic | ts.DiagnosticReporter | Diagnostic callback. |
| reportWatchStatus | ts.WatchStatusReporter | Watch status callback. |
| projectReferences | readonly ts.ProjectReference[] | Project graph inputs. |
| watchOptions | ts.WatchOptions | Watch strategy settings. |
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 = 7;");const c = controlledSystem();const root = path.resolve("main.ts");const host = ts.createWatchCompilerHost([root], { strict: true, noEmit: true }, c.system, ts.createSemanticDiagnosticsBuilderProgram, () => {}, () => {});assert.deepEqual(host.rootFiles, [root]);assert.equal(host.options.strict, true);assert.equal(c.active(), 0);console.log("host ready; no watchers yet");
// Run: node example.cjs// Expected stdout: host ready; no watchers yet