Diagnostic generation
Concept and boundary
A diagnostic combines a category, code, message text or chain and optional source location. The same code may be issued in different contexts. A tool should preserve structured data and format it separately; do not identify a diagnostic solely by its English wording.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: createDiagnosticForNode (line 17533). These are navigation anchors for source reading, not public API contracts. The probes below observe public behavior; they do not prove every internal step.
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: flatten-diagnostic-message-text
Revisit reference fixture ts-api-flatten-diagnostic-message-text (historical V23, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const chain = { messageText: "Contract mismatch", category: ts.DiagnosticCategory.Error, code: 0, next: [{ messageText: "Expected a number", category: ts.DiagnosticCategory.Error, code: 0 }] };const text = ts.flattenDiagnosticMessageText(chain, "\n");assert.equal(text, "Contract mismatch\n Expected a number");assert.equal(ts.flattenDiagnosticMessageText("Simple message", "\n"), "Simple message");console.log(text);Expected stdout (exit 0):
Contract mismatch Expected a numberWorkshop 3: diagnostic-span
const ts=require("typescript");const assert=require("node:assert/strict");const p=ts.createProgram(["input.ts"],{types:[],strict:true});const d=p.getSemanticDiagnostics().find(x=>x.code===2322);assert.ok(d && d.file && d.start!==undefined);const at=d.file.getLineAndCharacterOfPosition(d.start);assert.equal(at.line,0); assert.equal(at.character,6);console.log(`${d.code} ${at.line}:${at.character}`);Expected stdout (exit 0):
2322 0:6Practice and recap
Report a stable code plus file and span for an incompatible assignment, then compare the flattened message.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Diagnostic generation — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Diagnostic generation?
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 (flatten-diagnostic-message-text) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const chain = { messageText: "Contract mismatch", category: ts.DiagnosticCategory.Error, code: 0, next: [{ messageText: "Expected a number", category: ts.DiagnosticCategory.Error, code: 0 }] };const text = ts.flattenDiagnosticMessageText(chain, "\n");assert.equal(text, "Contract mismatch\n Expected a number");assert.equal(ts.flattenDiagnosticMessageText("Simple message", "\n"), "Simple message");console.log(text);What does workshop 3 (diagnostic-span) print when run unchanged with the specified compiler?
const ts=require("typescript");const assert=require("node:assert/strict");const p=ts.createProgram(["input.ts"],{types:[],strict:true});const d=p.getSemanticDiagnostics().find(x=>x.code===2322);assert.ok(d && d.file && d.start!==undefined);const at=d.file.getLineAndCharacterOfPosition(d.start);assert.equal(at.line,0); assert.equal(at.character,6);console.log(`${d.code} ${at.line}:${at.character}`);