This page documents the core data structures and APIs that developers interact with when using the TypeScript Compiler API. Understanding the relationship between the Abstract Syntax Tree (AST), the Type System (Types, Symbols, Signatures), and the Node Factory is essential for building tools like linters, refactoring engines, or custom transformers.
The AST is the foundational representation of source code. Every element in the source file, from a variable declaration to a semicolon, is represented as a Node.
A SourceFile is the root node of an AST representing a single file. It contains a collection of statements and metadata about the file (e.g., file name, language version).
kind (of type SyntaxKind), a pos (start position), and an end position src/compiler/types.ts28-31The primary way to interact with the AST is through traversal functions:
forEachChild: Invokes a callback for each immediate child of a node src/compiler/utilities.ts156findAncestor: Walks up the parent pointers to find a node matching a predicate src/compiler/utilities.ts146The following diagram maps common code constructs to their internal TypeScript AST representations.
Sources: src/compiler/types.ts40-420 src/compiler/parser.ts1-300
While the AST represents the syntax, the Type System represents the semantics. These objects are managed by the TypeChecker.
A Symbol represents a named declaration. Multiple declarations can contribute to the same symbol (e.g., a namespace and an interface with the same name).
Function, BlockScopedVariable, Interface) src/compiler/types.ts4365-4458A Type represents the TypeScript type assigned to an expression or symbol.
Interface, Reference, or Anonymous src/compiler/types.ts4503-4541A Signature represents a call, construct, or index signature.
This diagram illustrates how the compiler bridges a syntax node to its semantic meaning.
Sources: src/compiler/checker.ts1-135 src/compiler/types.ts4460-4765
The TypeChecker is the central engine for semantic queries. It is obtained via program.getTypeChecker() src/compiler/program.ts50
| Function | Purpose |
|---|---|
getSymbolAtLocation(node) | Retrieves the Symbol associated with a specific AST node. |
getTypeAtLocation(node) | Determines the Type of an expression node. |
getPropertiesOfType(type) | Returns an array of Symbol objects representing the members of a type. |
getSignatureFromDeclaration(node) | Gets the Signature for a function-like declaration. |
getDiagnostics() | Triggers full semantic analysis and returns errors. |
Sources: src/compiler/checker.ts117-135 src/compiler/types.ts4800-5300
The NodeFactory is the API used to create or update AST nodes. This is the only supported way to construct AST for use in transformers or code generation.
factory.update... methods which return a new node if changes are necessary.ts.factory or through the TransformationContext in a transformer src/compiler/factory/nodeFactory.ts1-100factory.createIdentifier(text): Creates a new identifier src/compiler/types.ts3100-3150factory.createVariableStatement(modifiers, declarationList): Creates a const/let/var statement.factory.updateBlock(node, statements): Updates an existing block with new statements.Sources: src/compiler/factory/nodeFactory.ts1-200 src/compiler/types.ts11-19
| Object | Created By | Lifetime | Role |
|---|---|---|---|
| Node | Parser / NodeFactory | Source-to-Emit | Syntactic representation of a code fragment. |
| Symbol | Binder | Compilation | Links declarations to a unique semantic identity. |
| Type | TypeChecker | On-demand (Lazy) | Represents the static type used for validation/inference. |
| Signature | TypeChecker | On-demand | Represents how a function or constructor can be called. |
Sources: src/compiler/parser.ts1-100 src/compiler/binder.ts1-50 src/compiler/checker.ts1-135
Refresh this wiki