Programs and compiler hosts
Concept and boundary
A Program combines root files, options and a CompilerHost; dependencies can enter through imports. A host controls file reading and output writing, whereas a Program owns the compilation context. Use numeric API enum members rather than JSON strings for compiler options.
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: create-program
Revisit reference fixture ts-api-create-program (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const roots = program.getRootFileNames();assert.deepEqual(roots, ["input.ts"]);assert(program.getSourceFiles().some(f => f.fileName.endsWith("dep.ts")));assert.equal(ts.getPreEmitDiagnostics(program).length, 0);console.log(roots.length, "dependency loaded");Expected stdout (exit 0):
1 dependency loadedWorkshop 2: create-compiler-host
Revisit reference fixture ts-api-create-compiler-host (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const host = ts.createCompilerHost(options);const outputs = new Map();host.writeFile = (name, text) => { outputs.set(name, text); };const program = ts.createProgram(["input.ts"], options, host);assert.equal(ts.getPreEmitDiagnostics(program).length, 0);assert.equal(program.emit().emitSkipped, false);assert.equal(outputs.size, 1);assert(!require("node:fs").existsSync("input.js"));console.log("captured", outputs.size);Expected stdout (exit 0):
captured 1Practice and recap
Add an imported dependency and record the root file list separately from all source files.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Programs and compiler hosts — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Programs and compiler hosts?
What does workshop 1 (create-program) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);const roots = program.getRootFileNames();assert.deepEqual(roots, ["input.ts"]);assert(program.getSourceFiles().some(f => f.fileName.endsWith("dep.ts")));assert.equal(ts.getPreEmitDiagnostics(program).length, 0);console.log(roots.length, "dependency loaded");What does workshop 2 (create-compiler-host) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const host = ts.createCompilerHost(options);const outputs = new Map();host.writeFile = (name, text) => { outputs.set(name, text); };const program = ts.createProgram(["input.ts"], options, host);assert.equal(ts.getPreEmitDiagnostics(program).length, 0);assert.equal(program.emit().emitSkipped, false);assert.equal(outputs.size, 1);assert(!require("node:fs").existsSync("input.js"));console.log("captured", outputs.size);