Compiler diagnostics API
Concept and boundary
Collect diagnostics from a Program before emission and use diagnostic codes plus locations to classify them. Syntax and semantic diagnostics are separate requests; getPreEmitDiagnostics aggregates pre-emission checking. Flatten a message chain for display without losing the diagnostic category in your own record.
Local tooling setup
For each workshop use a fresh folder. Install the pinned compiler with npm install --save-dev typescript@5.8.3, then create the listed files and run node example.cjs with Node.js 22.16.0. Assertions fail if the expected behavior changes. These are local JavaScript tooling scripts, not browser CodeRunner tasks; they do not independently type-check the tooling source.
Workshop 1: get-pre-emit-diagnostics
Revisit reference fixture ts-api-get-pre-emit-diagnostics (historical V23, rerun in V74).
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 codes = ts.getPreEmitDiagnostics(program).map(d => d.code);assert.deepEqual(codes, [2322]);console.log(codes.join(","));Expected stdout (exit 0):
2322Workshop 2: format-diagnostics
Revisit reference fixture ts-api-format-diagnostics (historical V23, rerun in V74).
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 host = { getCanonicalFileName: name => name, getCurrentDirectory: () => process.cwd(), getNewLine: () => "\n" };const text = ts.formatDiagnostics(ts.getPreEmitDiagnostics(program), host);assert(text.includes("input.ts(1,14): error TS2322:"));assert(text.includes("not assignable"));console.log("location and code present");Expected stdout (exit 0):
location and code presentPractice and recap
Record code, category and zero-based position for two independent errors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compiler diagnostics API — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Compiler diagnostics API?
What does workshop 1 (get-pre-emit-diagnostics) print when run unchanged with the specified compiler?
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 codes = ts.getPreEmitDiagnostics(program).map(d => d.code);assert.deepEqual(codes, [2322]);console.log(codes.join(","));What does workshop 2 (format-diagnostics) print when run unchanged with the specified compiler?
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 host = { getCanonicalFileName: name => name, getCurrentDirectory: () => process.cwd(), getNewLine: () => "\n" };const text = ts.formatDiagnostics(ts.getPreEmitDiagnostics(program), host);assert(text.includes("input.ts(1,14): error TS2322:"));assert(text.includes("not assignable"));console.log("location and code present");