TypeScript Compiler API
ts.createIncrementalProgram()
Constructs a builder from root names and compiler options, optionally reading existing compatible build information. Enable incremental or composite and use a valid cache path for persistent state. Creating the builder alone does not emit output or establish type correctness. Collect diagnostics and emit separately; cache state is disposable. 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.createIncrementalProgram<T extends ts.BuilderProgram = ts.EmitAndSemanticDiagnosticsBuilderProgram>(options: ts.IncrementalProgramOptions<T>): TParameters & Attributes
| Name | Type | Description |
|---|---|---|
| options | ts.IncrementalProgramOptions<T> | Root names, compiler options, optional host, factory, references and configuration diagnostics. |
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 = 7;");const builder = ts.createIncrementalProgram({ rootNames: [root], options });assert.deepEqual(ts.getPreEmitDiagnostics(builder.getProgram()), []);assert.equal(builder.emit().emitSkipped, false);assert(fs.existsSync("cache/state.tsbuildinfo"));assert(fs.existsSync("dist/main.d.ts"));console.log("build info and declarations created");
// Run: node example.cjs// Expected stdout: build info and declarations created