TypeScript Compiler API

ts.createSolutionBuilderWithWatch()

Creates a watch-enabled solution builder for referenced projects. Call build to initialize the project graph. Changes can invalidate downstream projects when public declarations change. In 5.8.3 the public SolutionBuilder interface does not expose close; this controlled example owns and clears its synthetic watcher registrations and timers, not a general shutdown recipe for native watchers. 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.createSolutionBuilderWithWatch<T extends ts.BuilderProgram>(host: ts.SolutionBuilderWithWatchHost<T>, rootNames: readonly string[], defaultOptions: ts.BuildOptions, baseWatchOptions?: ts.WatchOptions): ts.SolutionBuilder<T>

Parameters & Attributes

NameTypeDescription
hostts.SolutionBuilderWithWatchHost<T>Watch-capable build host.
rootNamesreadonly string[]Root configurations.
defaultOptionsts.BuildOptionsBuild settings.
baseWatchOptionsts.WatchOptionsOptional base watcher 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(); } };}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(), errors = [];const host = ts.createSolutionBuilderWithWatchHost(c.system, ts.createEmitAndSemanticDiagnosticsBuilderProgram, d=>errors.push(d.code), () => {}, () => {});const builder = ts.createSolutionBuilderWithWatch(host, ["tsconfig.json"], {});try {  assert.equal(builder.build(), ts.ExitStatus.Success);  assert(c.active() > 0);  c.change("lib/src/index.ts", 'export const value: string = "bad";'); c.drain();  assert(errors.includes(2322));  assert(fs.readFileSync("lib/dist/index.d.ts", "utf8").includes("value: string"));  console.log("upstream type change -> downstream 2322");} finally { c.clear(); }
// Run: node example.cjs// Expected stdout: upstream type change -> downstream 2322
TypeScript Reference