TypeScript Compiler API

ts.flattenDiagnosticMessageText()

Converts a string or nested diagnostic message chain into readable plain text with line breaks and indentation. This helper does not add a diagnostic code, filename or source coordinates. Use formatDiagnostics for a more complete user-facing diagnostic. 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
ts.flattenDiagnosticMessageText(diag, newLine, indent?): string

Parameters & Attributes

NameTypeDescription
diagstring | ts.DiagnosticMessageChain | undefinedMessage text or nested chain.
newLinestringLine separator used between messages.
indentnumberInitial indentation level.

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 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);
// Run: node example.cjs// Expected stdout: Contract mismatch//   Expected a number
TypeScript Reference