Programmatic emission
Concept and boundary
Program.emit writes generated artifacts through a write callback and returns an EmitResult. Declarations and JavaScript are different products; noEmitOnError can suppress both. Collect pre-emit diagnostics separately and inspect emitSkipped rather than relying on an empty output map alone.
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: program-emit
Revisit reference fixture ts-api-program-emit (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, declaration: true, noEmitOnError: true });assert.equal(ts.getPreEmitDiagnostics(program).length, 0);const outputs = new Map();const result = program.emit(undefined, (name, text) => { outputs.set(name, text); });assert.equal(result.emitSkipped, false);assert.equal(result.diagnostics.length, 0);assert(outputs.get("input.d.ts").includes("n: number"));assert(outputs.get("input.js").includes("exports.n = 7"));console.log([...outputs.keys()].sort().join(","));Expected stdout (exit 0):
input.d.ts,input.jsWorkshop 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
Capture declaration-only output, then introduce an incompatible assignment.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Programmatic emission — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Programmatic emission?
What does workshop 1 (program-emit) 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, declaration: true, noEmitOnError: true });assert.equal(ts.getPreEmitDiagnostics(program).length, 0);const outputs = new Map();const result = program.emit(undefined, (name, text) => { outputs.set(name, text); });assert.equal(result.emitSkipped, false);assert.equal(result.diagnostics.length, 0);assert(outputs.get("input.d.ts").includes("n: number"));assert(outputs.get("input.js").includes("exports.n = 7"));console.log([...outputs.keys()].sort().join(","));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);