TypeScript Compiler API

ts.TypeChecker

Provides semantic analysis for the nodes of a Program: types, symbols, signatures and relationships. A checker belongs to its program; do not use independently parsed nodes as if they were bound nodes of that program. typeToString is a display representation and not a stable canonical type identity. 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.TypeChecker; program.getTypeChecker(): ts.TypeChecker; checker.typeToString(type): string

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);const checker = program.getTypeChecker();const source = program.getSourceFile("input.ts");const fn = source.statements[0];assert(ts.isFunctionDeclaration(fn));const signature = checker.getSignatureFromDeclaration(fn);assert(signature);const type = checker.getReturnTypeOfSignature(signature);assert.equal(checker.typeToString(type), "number");console.log(checker.typeToString(type));
// File: input.ts (omit this label)export function twice(n: number) { return n * 2; }
// Run: node example.cjs// Expected stdout: number
TypeScript Reference