TypeScript Compiler API

ts.createEmitAndSemanticDiagnosticsBuilderProgram()

Creates a builder that manages affected-file diagnostics and incremental emission. Supply the prior compatible builder to reuse state within a process. The overload taking a Program also exists. Reuse does not guarantee that only the edited file is affected; declaration shape and dependencies matter. 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.createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: ts.CompilerOptions | undefined, host?: ts.CompilerHost, oldProgram?: ts.EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly ts.Diagnostic[], projectReferences?: readonly ts.ProjectReference[]): ts.EmitAndSemanticDiagnosticsBuilderProgram

Parameters & Attributes

NameTypeDescription
rootNamesreadonly string[] | undefinedRoot file paths; may be undefined for reuse with old builder.
optionsts.CompilerOptions | undefinedCurrent settings.
hostts.CompilerHostVersion-aware host.
oldProgramts.EmitAndSemanticDiagnosticsBuilderProgramPrevious compatible builder.
configFileParsingDiagnosticsreadonly ts.Diagnostic[]Configuration errors.
projectReferencesreadonly ts.ProjectReference[]Referenced projects.

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 = 1;");const host = ts.createIncrementalCompilerHost(options);let b = ts.createEmitAndSemanticDiagnosticsBuilderProgram([root], options, host);assert.deepEqual(b.getSemanticDiagnostics(), []); b.emit();put(root, "export const n: number = 2;");b = ts.createEmitAndSemanticDiagnosticsBuilderProgram([root], options, host, b);assert.deepEqual(b.getSemanticDiagnostics(), []); b.emit();assert.equal(require(path.resolve("dist/main.js")).n, 2);console.log("rebuilt value 2");
// Run: node example.cjs// Expected stdout: rebuilt value 2
TypeScript Reference