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.CreateProgramOptions
Options object for creating the program
readonly string[]
required
Array of root file names to include in the program
CompilerOptions
required
Compiler options for the program
CompilerHost
Custom compiler host for file system operations
Program
Previous program instance for incremental compilation
readonly Diagnostic[]
Diagnostics from parsing the config file
Example
Program Interface Methods
Source File Methods
getSourceFiles()
Returns all source files in the program.readonly SourceFile[]
Array of all source files in the program
getRootFileNames()
Returns the list of root file names passed tocreateProgram.
readonly string[]
Array of root file names
getSourceFile(fileName: string)
Returns a specific source file by name.string
required
The name of the source file to retrieve
SourceFile | undefined
The source file, or undefined if not found
Type Checking
getTypeChecker()
Returns a type checker that can be used to perform semantic analysis.TypeChecker
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.SourceFile
Optional source file to check. If not provided, checks all files.
CancellationToken
Optional token to cancel the operation
readonly DiagnosticWithLocation[]
Array of syntax diagnostics
getSemanticDiagnostics()
Returns semantic/type errors for a source file or all files.SourceFile
Optional source file to check
CancellationToken
Optional cancellation token
readonly Diagnostic[]
Array of semantic diagnostics
getDeclarationDiagnostics()
Returns errors related to declaration file generation.readonly DiagnosticWithLocation[]
Array of declaration diagnostics
getOptionsDiagnostics()
Returns diagnostics related to compiler options.readonly Diagnostic[]
Array of option diagnostics
getGlobalDiagnostics()
Returns global diagnostics not associated with any specific file.readonly Diagnostic[]
Array of global diagnostics
getConfigFileParsingDiagnostics()
Returns diagnostics from parsing the tsconfig.json file.readonly Diagnostic[]
Array of config file diagnostics
Emitting Output
emit()
Emits JavaScript and declaration files for the program.SourceFile
Optional specific file to emit. If not provided, emits all files.
WriteFileCallback
Optional custom write callback
CancellationToken
Optional cancellation token
boolean
If true, only emits declaration files
CustomTransformers
Optional custom AST transformers
EmitResult
Utility Methods
getCurrentDirectory()
Returns the current working directory.string
The current directory path
getNodeCount()
Returns the total number of AST nodes in the program.number
Total node count
getIdentifierCount()
Returns the total number of identifiers in the program.number
Total identifier count
getSymbolCount()
Returns the total number of symbols in the program.number
Total symbol count
getTypeCount()
Returns the total number of types in the program.number
Total type count
isSourceFileFromExternalLibrary(file: SourceFile)
Determines if a source file is from an external library.SourceFile
required
The source file to check
boolean
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).SourceFile
required
The source file to check
boolean
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.CompilerOptions
required
Compiler options to configure the host
boolean
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.string
required
TypeScript source code as a string
TranspileOptions
required
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.