The Program is the central orchestrator of the TypeScript compilation process. It manages source files, coordinates the compilation phases (scanning, parsing, binding, type checking, and emitting), and provides the primary API for compiling TypeScript code into JavaScript.
This document focuses on the Program interface and how it orchestrates the entire compilation pipeline. For details on individual compilation phases, see:
The Program interface is defined in src/compiler/types.ts3252-3341 and serves as the main entry point for TypeScript compilation. It provides methods to access source files, diagnostics, and trigger compilation.
Sources: src/compiler/types.ts3252-3341 src/compiler/program.ts1-150
| Method Category | Methods | Purpose |
|---|---|---|
| Source File Access | getSourceFiles(), getSourceFile(), getRootFileNames() | Access compiled source files |
| Compilation | emit(), getTypeChecker() | Trigger compilation and code generation |
| Diagnostics | getSemanticDiagnostics(), getSyntacticDiagnostics(), getDeclarationDiagnostics(), getGlobalDiagnostics() | Retrieve errors and warnings |
| Configuration | getCompilerOptions(), getCurrentDirectory(), getCommonSourceDirectory() | Access compilation settings |
| Project Structure | getProjectReferences(), getResolvedProjectReferences() | Multi-project compilation support |
Sources: src/compiler/types.ts3252-3341
Programs are created using the createProgram() factory function src/compiler/program.ts474-500 which orchestrates the initial setup and file discovery.
Sources: src/compiler/program.ts474-500 src/compiler/types.ts3369-3379
The CreateProgramOptions interface src/compiler/types.ts3369-3379 provides fine-grained control over program creation, including support for oldProgram to enable structure reuse.
The CompilerHost interface src/compiler/types.ts3344-3367 abstracts file system operations. Key methods include getSourceFile, writeFile, and resolveModuleNames.
The internal createProgram implementation in src/compiler/program.ts502-1530 follows a strict sequence to build the compilation context.
Sources: src/compiler/program.ts502-1530
The StructureIsReused enum src/compiler/types.ts3625-3629 determines whether an old program can be reused. The reuse logic in createProgram checks for compiler option compatibility and file changes src/compiler/program.ts1533-1729
The Program maintains a comprehensive map of all source files involved in compilation, including root files, library files, and discovered dependencies.
The discovery process in src/compiler/program.ts757-945 follows these steps:
rootNames.import and export statements.lib.d.ts based on the target.Each discovered file is loaded via CompilerHost.getSourceFile() src/compiler/program.ts983-1049 which triggers the Parser.
The Program coordinates compilation phases lazily, creating components like the TypeChecker only when requested.
Sources: src/compiler/program.ts1500-1530 src/compiler/program.ts2156-2279
Binding is triggered on first semantic access src/compiler/program.ts2156-2217 The TypeChecker is created via createTypeChecker(program) src/compiler/program.ts2232-2279 which is defined in src/compiler/checker.ts1-117
The Program provides multiple diagnostic collection methods, each serving a different purpose, such as getSyntacticDiagnostics for parse errors and getSemanticDiagnostics for type errors src/compiler/program.ts2638-2953
| Category | Source | Method |
|---|---|---|
| Syntactic | Parser | getSyntacticDiagnostics() |
| Global | Program | getGlobalDiagnostics() |
| Semantic | Type Checker | getSemanticDiagnostics() |
| Declaration | Emitter | getDeclarationDiagnostics() |
Sources: src/compiler/program.ts2638-2953
The emit() method src/compiler/program.ts3065-3229 orchestrates code generation by creating an EmitResolver and invoking emitFiles src/compiler/emitter.ts1-75
Sources: src/compiler/program.ts3065-3229 src/compiler/emitter.ts1-100
TypeScript supports incremental compilation through the BuilderProgram interface src/compiler/types.ts3631-3709 This allows for faster rebuilds by tracking affected files and reusing cached diagnostics.
Project references enable multi-project compilation with proper dependency ordering src/compiler/program.ts1100-1300 The Program resolves these via ResolvedProjectReference src/compiler/types.ts3602-3608
Sources: src/compiler/types.ts3631-3709 src/compiler/program.ts1100-1300
Refresh this wiki