Overview
TheProgram interface is the central component of the TypeScript Compiler API. It represents an immutable collection of source files and compiler options that form a compilation unit.
Creating a Program
ts.createProgram()
Creates a new Program instance.Options object for creating the program
Array of root file names to include in the program
Compiler options for the program
Custom compiler host for file system operations
Previous program instance for incremental compilation
Diagnostics from parsing the config file
Example
Program Interface Methods
Source File Methods
getSourceFiles()
Returns all source files in the program.Array of all source files in the program
getRootFileNames()
Returns the list of root file names passed tocreateProgram.
Array of root file names
getSourceFile(fileName: string)
Returns a specific source file by name.The name of the source file to retrieve
The source file, or undefined if not found
Type Checking
getTypeChecker()
Returns a type checker that can be used to perform semantic analysis.A TypeChecker instance for semantic analysis
Diagnostics
The Program provides several methods for retrieving compilation diagnostics.getSyntacticDiagnostics()
Returns syntax errors for a source file or all files.Optional source file to check. If not provided, checks all files.
Optional token to cancel the operation
Array of syntax diagnostics
getSemanticDiagnostics()
Returns semantic/type errors for a source file or all files.Optional source file to check
Optional cancellation token
Array of semantic diagnostics
getDeclarationDiagnostics()
Returns errors related to declaration file generation.Array of declaration diagnostics
getOptionsDiagnostics()
Returns diagnostics related to compiler options.Array of option diagnostics
getGlobalDiagnostics()
Returns global diagnostics not associated with any specific file.Array of global diagnostics
getConfigFileParsingDiagnostics()
Returns diagnostics from parsing the tsconfig.json file.Array of config file diagnostics
Emitting Output
emit()
Emits JavaScript and declaration files for the program.Optional specific file to emit. If not provided, emits all files.
Optional custom write callback
Optional cancellation token
If true, only emits declaration files
Optional custom AST transformers
Utility Methods
getCurrentDirectory()
Returns the current working directory.The current directory path
getNodeCount()
Returns the total number of AST nodes in the program.Total node count
getIdentifierCount()
Returns the total number of identifiers in the program.Total identifier count
getSymbolCount()
Returns the total number of symbols in the program.Total symbol count
getTypeCount()
Returns the total number of types in the program.Total type count
isSourceFileFromExternalLibrary(file: SourceFile)
Determines if a source file is from an external library.The source file to check
True if the file is from an external library
isSourceFileDefaultLibrary(file: SourceFile)
Determines if a source file is from the default library (e.g., lib.d.ts).The source file to check
True if the file is from the default library
Creating a Compiler Host
ts.createCompilerHost()
Creates a default CompilerHost implementation that uses the file system.Compiler options to configure the host
Whether to set parent nodes in the AST (default: false)
CompilerHost instance with default file system operations.
Example
Transpiling Single Files
ts.transpileModule()
Transpiles a single TypeScript source string to JavaScript without type checking.TypeScript source code as a string
TranspileOutput with properties:
outputText: The transpiled JavaScript codediagnostics: Optional array of diagnosticssourceMapText: Optional source map as a string
Example
transpileModule is fast but doesn’t perform type checking. Use createProgram with a full compilation for type checking.