TypeScript Compiler API

ts.BuilderProgram

A builder wraps the current Program and manages incremental work and cached diagnostics. Its concrete variants have different affected-file operations. Use the current program for semantic queries; stale nodes from an earlier program may not represent current source. Whole-program diagnostic queries are not the same as iterating pending affected files. 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.BuilderProgram; getProgram(): ts.Program; getSemanticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken): readonly ts.Diagnostic[]

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);}const options = { strict: true, target: ts.ScriptTarget.ES2022,  module: ts.ModuleKind.CommonJS, types: [], incremental: true,  declaration: true, outDir: "dist", tsBuildInfoFile: "cache/state.tsbuildinfo" };const root = path.resolve("main.ts");put(root, 'export const n: number = "bad";');const b = ts.createIncrementalProgram({ rootNames: [root], options });assert.equal(b.getProgram().getSourceFile(root), b.getSourceFile(root));assert.deepEqual(b.getSemanticDiagnostics().map(d => d.code), [2322]);console.log("builder diagnostic 2322");
// Run: node example.cjs// Expected stdout: builder diagnostic 2322
TypeScript Reference