This page documents the overall architecture of the TypeScript compiler, including its four main phases (scanning, parsing, binding, and type checking) and the role of the emitter and program orchestration. This covers the core compilation pipeline that transforms TypeScript source code into JavaScript output and type declarations.
For information about specific language service features used by editors, see Language Service. For details on the transformation pipeline that downlevels modern JavaScript, see Transformation Pipeline. For the emitter's detailed operation, see Emitter.
The TypeScript compiler follows a traditional multi-phase architecture where each phase builds upon the previous one:
Sources: src/compiler/types.ts1-100 src/compiler/program.ts1-100
The compilation process is orchestrated by the Program interface, which manages source files and coordinates the execution of each phase. Each phase operates on the Abstract Syntax Tree (AST) created by the parser, progressively enriching it with semantic information.
The SyntaxKind enum is the foundation of the AST, defining all possible node types in TypeScript:
Sources: src/compiler/types.ts40-492
Every node in the AST has a kind property of type SyntaxKind that determines its type. This ranges from simple tokens (SyntaxKind.Identifier src/compiler/types.ts133 SyntaxKind.StringLiteral src/compiler/types.ts58) to complex declarations (SyntaxKind.ClassDeclaration src/compiler/types.ts288 SyntaxKind.FunctionDeclaration src/compiler/types.ts287).
All AST elements extend the Node interface:
| Property | Type | Description |
|---|---|---|
kind | SyntaxKind | Discriminant for the node type |
pos | number | Start position including trivia |
end | number | End position |
flags | NodeFlags | Syntactic context flags |
parent | Node | Parent node reference |
symbol | Symbol | Associated symbol (set by binder) |
Sources: src/compiler/types.ts778-825
The scanner (createScanner()) converts source text into a stream of tokens. It is a stateful component that maintains position and token state.
Sources: src/compiler/scanner.ts28-100 src/compiler/scanner.ts412-850
The scanner is created via createScanner() defined in src/compiler/scanner.ts412 Key characteristics:
setText() src/compiler/scanner.ts446setTextPos() src/compiler/scanner.ts466 to resume from arbitrary positionslookAhead() function src/compiler/scanner.ts805 enables speculative scanningFor details, see Scanner.
The parser converts the token stream into an Abstract Syntax Tree (AST). The entry point is parseSourceFile() src/compiler/parser.ts1037 which returns a SourceFile node.
Sources: src/compiler/parser.ts1037-2500
NodeFactory: The parser uses a factory to create AST nodes, providing methods like createIdentifier() and createBinaryExpression().
For details, see Parser.
The binder walks the AST to create symbols, establish lexical scopes, and build control flow graphs. The entry point is bindSourceFile() src/compiler/binder.ts511
Sources: src/compiler/binder.ts511-1200
The binder creates Symbol objects for declarations src/compiler/binder.ts1535 and building symbol tables using createSymbolTable() src/compiler/binder.ts42
For details, see Binder.
The type checker performs semantic analysis, type inference, and diagnostic generation. It is created via createTypeChecker() src/compiler/checker.ts600
Sources: src/compiler/checker.ts600-2000
The type checker resolves names to symbols src/compiler/checker.ts15000 and symbols to types src/compiler/checker.ts20000
For details, see Type Checker.
The emitter generates JavaScript code, declaration files, and source maps. The entry point is emitFiles() src/compiler/emitter.ts2856
Sources: src/compiler/emitter.ts2856-3200
For details, see Emitter.
The Program interface src/compiler/types.ts3975 orchestrates the entire compilation process and manages source files. It is created via createProgram() src/compiler/program.ts2000
| Method | Description |
|---|---|
getSourceFiles() | Returns all files in the program |
getTypeChecker() | Returns the type checker instance |
emit() | Triggers the emitter phase |
getSemanticDiagnostics() | Triggers type checking and returns errors |
Sources: src/compiler/types.ts3975-4150
For details, see Program and Compilation.
| Phase | Input | Output | Key Data Structures |
|---|---|---|---|
| Scanner | Source text | Token stream | SyntaxKind src/compiler/types.ts40 |
| Parser | Token stream | AST | SourceFile src/compiler/types.ts3276 |
| Binder | AST | Symbols + Flow | Symbol src/compiler/types.ts4846 FlowNode src/compiler/types.ts3737 |
| Type Checker | Symbols + AST | Types + Diagnostics | Type src/compiler/types.ts5266 Diagnostic src/compiler/types.ts3561 |
| Emitter | Typed AST | Output files | .js, .d.ts, .map |
Sources: src/compiler/types.ts1-6000
Refresh this wiki