Node factories and printing
Concept and boundary
Use ts.factory to construct synthetic nodes and createPrinter to turn them into source text. The printer needs the appropriate EmitHint and source context. Printing an annotated function keeps its TypeScript annotation; it is not JavaScript compilation.
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-function-declaration
Revisit reference fixture ts-api-create-function-declaration (historical V24, rerun in V74).
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 f = ts.factory;const number = f.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);const param = f.createParameterDeclaration(undefined, undefined, "n", undefined, number);const body = f.createBlock([f.createReturnStatement(f.createBinaryExpression(f.createIdentifier("n"), ts.SyntaxKind.AsteriskToken, f.createNumericLiteral(2)))], true);const fn = f.createFunctionDeclaration(undefined, undefined, "twice", undefined, [param], number, body);assert(ts.isFunctionDeclaration(fn));console.log(printer.printNode(ts.EmitHint.Unspecified, fn, source));Expected stdout (exit 0):
function twice(n: number): number { return n * 2;}Workshop 2: emit-hint
Revisit reference fixture ts-api-emit-hint (historical V24, rerun in V74).
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 identifier = ts.factory.createIdentifier("score");const expression = ts.factory.createNumericLiteral(4);console.log(printer.printNode(ts.EmitHint.IdentifierName, identifier, source));console.log(printer.printNode(ts.EmitHint.Expression, expression, source));Expected stdout (exit 0):
score4Practice and recap
Create a function taking two numbers instead of one and print its AST.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Node factories and printing — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Node factories and printing?
What does workshop 1 (create-function-declaration) print when run unchanged with the specified compiler?
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 f = ts.factory;const number = f.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);const param = f.createParameterDeclaration(undefined, undefined, "n", undefined, number);const body = f.createBlock([f.createReturnStatement(f.createBinaryExpression(f.createIdentifier("n"), ts.SyntaxKind.AsteriskToken, f.createNumericLiteral(2)))], true);const fn = f.createFunctionDeclaration(undefined, undefined, "twice", undefined, [param], number, body);assert(ts.isFunctionDeclaration(fn));console.log(printer.printNode(ts.EmitHint.Unspecified, fn, source));What does workshop 2 (emit-hint) print when run unchanged with the specified compiler?
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 identifier = ts.factory.createIdentifier("score");const expression = ts.factory.createNumericLiteral(4);console.log(printer.printNode(ts.EmitHint.IdentifierName, identifier, source));console.log(printer.printNode(ts.EmitHint.Expression, expression, source));