TypeScript Compiler API

TransformationResult.dispose()

Releases transformation-associated emit metadata after consuming a standalone ts.transform result. Use try/finally even if printing or subsequent processing throws. Disposal is not a substitute for releasing references to large trees; it does not promise that retained nodes become invalid or that garbage collection runs immediately. 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
result.dispose(): void

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 source = ts.createSourceFile("input.ts", "const count: number = 7;", ts.ScriptTarget.ES2022, true);const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });const replaceSeven = context => root => {  function visit(node) {    if (ts.isNumericLiteral(node) && node.text === "7") return context.factory.createNumericLiteral(8);    return ts.visitEachChild(node, visit, context);  }  return ts.visitNode(root, visit, ts.isSourceFile);};const result = ts.transform(source, [replaceSeven]);let observed = false;try {  assert(printer.printFile(result.transformed[0]).includes("= 8"));  throw new Error("simulated consumer failure");} catch (e) { assert.equal(e.message, "simulated consumer failure"); observed = true; }finally { result.dispose(); console.log("disposed"); }assert(observed);
// Run: node example.cjs// Expected stdout: disposed
TypeScript Reference