API Reference

TypeScript

Typed superset of JavaScript whose compiler API exposes program creation, type checking, and single-file transpilation.

Official Documentation ↗
CompatibilityThis documentation covers version v5. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev typescript
pnpmpnpm add -D typescript

Methods

Overview

TypeScript is a typed superset of JavaScript whose compiler API lets tools create programs, run type checks, and transpile files individually. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • ts.createProgram — Creates a Program covering the given roots, enabling full semantic type checking.
  • ts.transpileModule — Transpiles a single file to JavaScript without type checking, ideal for fast tooling.
  • ts.createSourceFile — Produces an AST for lightweight parsing and codemod scenarios.

Example

const ts = require('typescript');

const program = ts.createProgram(['src/index.ts'], {
  strict: true,
  noEmit: true,
});
const diagnostics = ts.getPreEmitDiagnostics(program);

Notes

The full Program API performs semantic analysis and is slower, while transpileModule is fast but skips type checking. Choose based on whether your tool needs type errors.

Verification

Create a program with a deliberate type error and confirm getPreEmitDiagnostics returns a non-empty list with the expected code.

FAQ

Does transpileModule perform type checking?
No. transpileModule only strips and downlevels syntax; use createProgram plus getPreEmitDiagnostics for real type errors.
How do I report compiler errors from the API?
Call ts.getPreEmitDiagnostics(program) and format each diagnostic with ts.flattenDiagnosticMessageText and the message text.
What is the difference between languageVersion and target?
languageVersion controls how the parser interprets source syntax, while compilerOptions.target controls the emitted JavaScript level.