TypeScript Compiler API

ts.CustomTransformers

Groups transformer factories for compiler emission. Before hooks run before built-in JavaScript transforms, after hooks after those transforms, and afterDeclarations hooks after built-in declaration transforms. Not every hook runs in every emission mode. These hooks do not automatically recheck transformed output; validate the emitted runtime and declaration contracts separately. Baseline: the Node.js API of TypeScript 5.8.3. The examples are CommonJS tooling scripts, not browser or CodeRunner snippets. TypeScript must be installed locally.

Formal Syntax & Structure

typescript syntaxtypescript
interface ts.CustomTransformers { before?; after?; afterDeclarations?; }; program.emit(targetSourceFile?, writeFile?, cancellationToken?, emitOnlyDtsFiles?, customTransformers?)

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 options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmitOnError: true, declaration: true };const program = ts.createProgram(["input.ts"], options);assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []);const fs = require("node:fs");const outputs = new Map();const write = (name, text) => outputs.set(name, text);const order = [];const mark = stage => context => root => { order.push(stage); return root; };const result = program.emit(undefined, write, undefined, false, { before: [mark("before")], after: [mark("after")], afterDeclarations: [mark("declarations")] });assert(!result.emitSkipped);assert(outputs.has("input.js") && outputs.has("input.d.ts"));assert.deepEqual(order, ["before", "after", "declarations"]);console.log(order.join(" -> "));
// File: input.ts (omit this label)export const count: number = 7;
// Run: node example.cjs// Expected stdout: before -> after -> declarations
// Separate variation in a fresh directory.// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmitOnError: true, declaration: true };const program = ts.createProgram(["input.ts"], options);assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []);const fs = require("node:fs");const outputs = new Map();const write = (name, text) => outputs.set(name, text);const order = [];const mark = stage => context => root => { order.push(stage); return root; };const result = program.emit(undefined, write, undefined, true, { before: [mark("before")], after: [mark("after")], afterDeclarations: [mark("declarations")] });assert(!result.emitSkipped);assert.deepEqual([...outputs.keys()], ["input.d.ts"]);assert.deepEqual(order, ["declarations"]);console.log(order.join(","));
// File: input.ts (omit this label)export const count: number = 7;
// Run: node example.cjs// Expected stdout: declarations
TypeScript Reference