TypeScript Compiler API

ts.Program

Represents a compiler compilation context, including root files, dependencies and library declarations. Root files are not the same as every source file. It exposes diagnostic, checker and emission operations. Treat a Program and its nodes as a snapshot rather than mutating source files in place and expecting fresh results. Baseline: the Node.js API of TypeScript 5.8.3. The examples are CommonJS tooling scripts, not browser or CodeRunner snippets. TypeScript must be installed locally.

Formal Syntax & Structure

typescript syntaxtypescript
interface ts.Program; program.getRootFileNames(); program.getSourceFiles(); program.getTypeChecker()

Working Example

Example Snippettypescript
// Use a new empty directory. Install: npm install --save-exact typescript@5.8.3// File: example.cjs (omit this label)const ts = require("typescript");const assert = require("node:assert/strict");const options = { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS };const program = ts.createProgram(["input.ts"], options);assert.equal(program.getRootFileNames().length, 1);const input = program.getSourceFile("input.ts");assert(input && ts.isSourceFile(input));const all = program.getSourceFiles();assert(all.some(f => f.isDeclarationFile));console.log("root", input.fileName, "libraries included", all.some(f => f.isDeclarationFile));
// File: input.ts (omit this label)export const n = 7;
// Run: node example.cjs// Expected stdout: root input.ts libraries included true
TypeScript Reference