TypeScript Compiler API
ts.createSolutionBuilderWithWatchHost()
Creates a solution host with watch registrations and status reporting in addition to build services. Pass it to createSolutionBuilderWithWatch and call build to begin the solution workflow. Host creation alone does not start file watchers. The fixture uses controlled registrations rather than real OS 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
ts.createSolutionBuilderWithWatchHost<T extends ts.BuilderProgram = ts.EmitAndSemanticDiagnosticsBuilderProgram>(system?: ts.System, createProgram?: ts.CreateProgram<T>, reportDiagnostic?: ts.DiagnosticReporter, reportSolutionBuilderStatus?: ts.DiagnosticReporter, reportWatchStatus?: ts.WatchStatusReporter): ts.SolutionBuilderWithWatchHost<T>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| system | ts.System | Watch-capable system. |
| createProgram | ts.CreateProgram<T> | Builder factory. |
| reportDiagnostic | ts.DiagnosticReporter | Diagnostic callback. |
| reportSolutionBuilderStatus | ts.DiagnosticReporter | Solution build status callback. |
| reportWatchStatus | ts.WatchStatusReporter | Watch status 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(); } };}function solution() { const opts = { composite: true, declaration: true, strict: true, target: "ES2022", module: "CommonJS", types: [], rootDir: "src", outDir: "dist" }; put("lib/tsconfig.json", JSON.stringify({ compilerOptions: opts, files: ["src/index.ts"] })); put("lib/src/index.ts", "export const value: number = 7;"); put("app/tsconfig.json", JSON.stringify({ compilerOptions: opts, files: ["src/index.ts"], references: [{ path: "../lib" }] })); put("app/src/index.ts", 'import { value } from "../../lib/src/index"; export const result: number = value;'); put("tsconfig.json", JSON.stringify({ files: [], references: [{ path: "./app" }] }));}solution();const c = controlledSystem();const host = ts.createSolutionBuilderWithWatchHost(c.system, ts.createEmitAndSemanticDiagnosticsBuilderProgram, () => {}, () => {}, () => {});assert.equal(typeof host.watchFile, "function");assert.equal(typeof host.setTimeout, "function");assert.equal(c.active(), 0);console.log("watch solution host ready");c.clear();
// Run: node example.cjs// Expected stdout: watch solution host ready