0Pricing
TypeScript Academy · Lesson

Creating a TypeScript Program with the API

Initialize a compiler program and access the type checker.

Creating a TypeScript Program with the API is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is the Compiler API?

TypeScript exposes its compiler internals through the typescript npm package. You can create programs, inspect the type checker, and analyze source files programmatically.

import ts from "typescript";
// Use ts.createProgram to start

Installing the typescript Package

Install TypeScript as a regular dependency (not just devDependency) when using the compiler API in your tools.

npm install typescript
# Also install types if needed
npm install --save-dev @types/node

Creating a Program

ts.createProgram takes an array of entry file paths and compiler options and returns a Program object.

import ts from "typescript";
const program = ts.createProgram(["src/index.ts"], {
  target: ts.ScriptTarget.ES2020,
  module: ts.ModuleKind.CommonJS,
  strict: true,
});

Accessing Source Files

Use program.getSourceFiles() to get all files in the compilation, or getSourceFile for a specific path.

const sourceFiles = program.getSourceFiles();
sourceFiles.forEach(sf => {
  if (!sf.isDeclarationFile) {
    console.log(sf.fileName);
  }
});

Getting the Type Checker

The type checker is the core of TypeScript analysis. Obtain it with program.getTypeChecker().

const checker = program.getTypeChecker();
// Now use checker to resolve types, symbols, etc.

Reading Diagnostics

Retrieve compiler diagnostics (errors and warnings) to implement custom validation tools.

const diagnostics = ts.getPreEmitDiagnostics(program);
diagnostics.forEach(d => {
  const msg = ts.flattenDiagnosticMessageText(d.messageText, "
");
  console.log(msg);
});

Reading tsconfig.json

Use ts.readConfigFile and ts.parseJsonConfigFileContent to load compiler options from a tsconfig file.

const cfgFile = ts.readConfigFile("tsconfig.json", ts.sys.readFile);
const parsed = ts.parseJsonConfigFileContent(
  cfgFile.config,
  ts.sys,
  process.cwd()
);
const program2 = ts.createProgram(parsed.fileNames, parsed.options);

Symbol Resolution

The type checker can resolve symbols — names that refer to declarations — to inspect what a given identifier points to.

const sf = program.getSourceFile("src/index.ts")!;
ts.forEachChild(sf, node => {
  if (ts.isVariableStatement(node)) {
    const symbol = checker.getSymbolAtLocation(
      node.declarationList.declarations[0].name
    );
    console.log(symbol?.getName());
  }
});

Emitting Output

Call program.emit() to produce JavaScript and declaration files, just like running tsc.

const emitResult = program.emit();
const exitCode = emitResult.emitSkipped ? 1 : 0;
process.exit(exitCode);

Incremental Programs

Use ts.createIncrementalProgram for faster repeated analysis by reusing previous build state.

// Incremental compilation tracks which files changed
// to avoid reprocessing unchanged files

Recap: Compiler API Basics

The TypeScript compiler API lets you create programs, inspect source files, query types through the checker, and emit JavaScript — all from Node.js code.

Quick Check

Which method returns the TypeScript type checker from a program?

What You Learned

The TypeScript compiler API starts with ts.createProgram. From the program you can access source files, the type checker, diagnostics, and emit output — the foundation for building linters, code generators, and analysis tools.

Frequently asked questions

Is the “Creating a TypeScript Program with the API” lesson free?

Yes — the full text of “Creating a TypeScript Program with the API” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Creating a TypeScript Program with the API”?

Initialize a compiler program and access the type checker. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating a TypeScript Program with the API” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a TypeScript Program with the API
  2. Traversing the AST with Visitors
  3. Custom Transformers and Code Generation
  4. Building a Simple Linting Tool
← Back to TypeScript Academy