The TypeScript compiler API provides a programmatic interface for orchestrating the compilation process. This allows developers to integrate TypeScript's type checking, transformation, and code generation into custom build tools, linters, and IDE extensions. The primary entry point for this process is the Program object, which coordinates the interaction between source files, compiler options, and the host environment.
The programmatic compilation process typically follows a standard sequence: parsing configuration, creating a host to interface with the file system, and instantiating a Program to execute the compiler phases (parsing, binding, checking, and emitting).
The following diagram illustrates the flow from configuration to final output.
"Compilation Data Flow"
Sources: src/compiler/program.ts1-50 src/compiler/commandLineParser.ts1-100
Before compiling, the compiler must resolve the configuration. While CompilerOptions can be created manually, most tools use parseJsonConfigFileContent to convert a raw tsconfig.json object into a ParsedCommandLine object.
parseJsonConfigFileContent src/compiler/commandLineParser.ts1-2550extends), resolves glob patterns in include/exclude, and validates that the options are mutually compatible.The CompilerHost is an abstraction layer that the Program uses to interact with the environment (e.g., reading files, checking case sensitivity, resolving modules).
createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost src/compiler/program.ts1000-1100sys (the system abstraction) to access the physical disk. However, for in-memory compilation (like in a web browser or test runner), a custom host can be provided.The Program is the central coordinator. It is immutable; if files change, a new Program must be created (potentially using an old program as a baseline for incremental speed).
createProgram(rootNames: readonly string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program, configFileParsingDiagnostics?: readonly Diagnostic[]): Program src/compiler/program.ts1200-1300SourceFile objects via the parser, and initializes the TypeChecker.Hosts decouple the compiler logic from the underlying platform (Node.js, Browser, VS Code).
| Interface | Purpose | Key Methods |
|---|---|---|
System | Low-level OS abstraction. | readFile, writeFile, directoryExists |
CompilerHost | High-level compiler environment. | getSourceFile, getDefaultLibFileName, resolveModuleNames |
LanguageServiceHost | Used for IDE features. | getScriptSnapshot, getScriptVersion |
Sources: src/compiler/sys.ts1-100 src/compiler/types.ts4000-4500
The Program instance orchestrates the four main phases of the compiler.
The program calls host.getSourceFile. This triggers the createSourceFile function in parser.ts, which converts text into an Abstract Syntax Tree (AST).
createSourceFile src/compiler/parser.ts50-150Once the AST is ready, the Binder walks the tree to identify symbols and create the scope hierarchy. This is necessary before type checking can begin.
bindSourceFile src/compiler/binder.ts10-100The TypeChecker is instantiated via createTypeChecker. It performs semantic analysis, calculates types, and produces Diagnostic objects.
createTypeChecker src/compiler/checker.ts100-500Finally, the Emitter transforms the AST (applying transformers for things like JSX or decorators) and writes the output files.
emitFiles src/compiler/emitter.ts1-200The following diagram maps the high-level compilation stages to the specific internal functions and classes that handle them.
"Compiler API Internal Mapping"
Sources: src/compiler/types.ts1-500 src/compiler/program.ts500-1000 src/compiler/checker.ts1-200
This sequence demonstrates how the core functions interact to produce JavaScript output.
Sources: src/testRunner/unittests/publicApi.ts1-100 src/compiler/program.ts100-300
Diagnostics are gathered from multiple stages:
Sources: src/compiler/program.ts1400-1800 src/compiler/types.ts1-100
Refresh this wiki