Incremental state and caching
Concept and boundary
Builder state records information about source versions and dependencies; a change to an exported contract can invalidate consumers. Persistent build information can seed a later compatible build, but not every change requires checking the same files. Inspect affected diagnostics and treat serialized state as disposable.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: createBuilderProgram (line 130567). These are navigation anchors for source reading, not public API contracts. The probes below observe public behavior; they do not prove every internal step.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: incremental-state-management
Revisit reference fixture ts-api-incremental-state-management (historical V26, rerun in V74).
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 = { readFile: ts.sys.readFile, getCurrentDirectory: ts.sys.getCurrentDirectory, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames };assert.equal(ts.readBuilderProgram(options, host), undefined);let b = ts.createIncrementalProgram({ rootNames: [root], options });assert.deepEqual(b.getSemanticDiagnostics(), []); b.emit();assert(ts.readBuilderProgram(options, host));put(root, 'export const n: number = "bad";');b = ts.createIncrementalProgram({ rootNames: [root], options });assert.deepEqual(b.getSemanticDiagnostics().map(d=>d.code), [2322]);console.log("missing -> readable cache -> 2322");Expected stdout (exit 0):
missing -> readable cache -> 2322Workshop 2: affected-file-diagnostics
Revisit reference fixture ts-api-affected-file-diagnostics (historical V26, rerun in V74).
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 host = ts.createIncrementalCompilerHost(options);const b = ts.createSemanticDiagnosticsBuilderProgram([root], options, host);const errors = [];let item;while ((item = b.getSemanticDiagnosticsOfNextAffectedFile()) !== undefined) { if (item.affected === b.getSourceFile(root)) errors.push(...item.result.map(d=>d.code));}assert.deepEqual(errors, [2322]);assert.equal(b.getSemanticDiagnosticsOfNextAffectedFile(), undefined);console.log("affected main.ts: 2322; drained");Expected stdout (exit 0):
affected main.ts: 2322; drainedPractice and recap
Change a function body without changing its exported type, then compare an exported type change.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Incremental state and caching — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Incremental state and caching?
What does workshop 1 (incremental-state-management) print when run unchanged with the specified compiler?
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 = { readFile: ts.sys.readFile, getCurrentDirectory: ts.sys.getCurrentDirectory, useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames };assert.equal(ts.readBuilderProgram(options, host), undefined);let b = ts.createIncrementalProgram({ rootNames: [root], options });assert.deepEqual(b.getSemanticDiagnostics(), []); b.emit();assert(ts.readBuilderProgram(options, host));put(root, 'export const n: number = "bad";');b = ts.createIncrementalProgram({ rootNames: [root], options });assert.deepEqual(b.getSemanticDiagnostics().map(d=>d.code), [2322]);console.log("missing -> readable cache -> 2322");What does workshop 2 (affected-file-diagnostics) print when run unchanged with the specified compiler?
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 host = ts.createIncrementalCompilerHost(options);const b = ts.createSemanticDiagnosticsBuilderProgram([root], options, host);const errors = [];let item;while ((item = b.getSemanticDiagnosticsOfNextAffectedFile()) !== undefined) { if (item.affected === b.getSourceFile(root)) errors.push(...item.result.map(d=>d.code));}assert.deepEqual(errors, [2322]);assert.equal(b.getSemanticDiagnosticsOfNextAffectedFile(), undefined);console.log("affected main.ts: 2322; drained");