TypeScript Compiler API

ts.createIncrementalCompilerHost()

Creates a compiler host suitable for incremental builders. Its source files carry versions used for change detection. An optional System supplies filesystem operations; keep its reads and identity semantics consistent. Do not substitute an ordinary unversioned host without providing source versions. 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.createIncrementalCompilerHost(options: ts.CompilerOptions, system?: ts.System): ts.CompilerHost

Parameters & Attributes

NameTypeDescription
optionsts.CompilerOptionsCompiler settings using API values.
systemts.SystemOptional filesystem and environment abstraction.

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 = 1;");const host = ts.createIncrementalCompilerHost(options);const a = host.getSourceFile(root, options.target);assert.equal(typeof a.version, "string");put(root, "export const n = 2;");const b = host.getSourceFile(root, options.target);assert.notEqual(a.version, b.version);console.log("source version changed");
// Run: node example.cjs// Expected stdout: source version changed
TypeScript Reference