Node creation
- TypeScript course
- Expert
- Lesson 6/35
- 4 min
Learn to produce AST nodes with ts.factory.
What is ts.factory?
ts.factory is the object in the TypeScript compiler that produces new AST nodes. It has methods like createIdentifier, createNumericLiteral and createStringLiteral for every kind of node. It replaces the older ts.createNode-style API.
const id = ts.factory.createIdentifier("merhaba");
const num = ts.factory.createNumericLiteral("42");
const str = ts.factory.createStringLiteral("selam");The produced nodes are used inside transformers or in hand-built ASTs.
Combining nodes
You combine nodes to build complex expressions. For console.log(42) you build: console.log with createPropertyAccessExpression, the call with createCallExpression, and the statement with createExpressionStatement.
const call = ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("console"),
ts.factory.createIdentifier("log")
),
undefined,
[ts.factory.createNumericLiteral("42")]
);Practice Build a console.log(42) node
Build console.log(42) with ts.factory: produce console.log with PropertyAccessExpression, the call with CallExpression, and the statement with ExpressionStatement. Print the result with ts.createPrinter and printNode.
Starter code
import * as ts from "typescript";
// console.log(42) çağrısı oluştur:
// - PropertyAccessExpression: console.log
// - CallExpression: console.log(42)
// - ExpressionStatement ile sar
// factory ile düğümleri üret
// Printer ile sonucu yazdır
// kodunu buraya yazSample solution
import ts from "typescript";
const dummy = ts.createSourceFile("dummy.ts", "", ts.ScriptTarget.Latest, true);
const call = ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("console"),
ts.factory.createIdentifier("log")
),
undefined,
[ts.factory.createNumericLiteral("42")]
);
const stmt = ts.factory.createExpressionStatement(call);
const printer = ts.createPrinter();
console.log(printer.printNode(ts.EmitHint.Unspecified, stmt, dummy));Expected output
console.log(42);
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does ts.factory.createIdentifier("x") return?
- An Identifier node Correct answer
- A Symbol object
- A string value
- A Type object
Explanation: createIdentifier returns a new Identifier AST node, not a string, Type or Symbol.
2 Create a numeric node
const num = ts.factory.____("42");
const num = ts.factory.createNumericLiteral("42");
Explanation: createNumericLiteral produces a NumericLiteral node; createNumber and createNumberLiteral do not exist on the factory and createLiteral is a different API.
3 factory parts
| Term | Meaning |
|---|---|
ts.factory.createIdentifier |
Produces an Identifier node |
ts.factory.createNumericLiteral |
Produces a NumericLiteral node |
ts.factory.createCallExpression |
Produces a call expression |
ts.factory.createExpressionStatement |
Produces a statement |