TypeScript Compiler API
getSemanticDiagnosticsOfNextAffectedFile()
Iterates pending affected-file semantic diagnostics on a semantic diagnostics builder until undefined. The affected value may be a SourceFile or Program; use the result diagnostics without assuming a file shape. Iteration advances builder state, so drain it deliberately and avoid mixing unrelated queries when demonstrating the queue. An initial build can include library files; filter only for reporting, not to bypass checking. Baseline: TypeScript 5.8.3, Node.js. Run as a local tooling script in a disposable directory; examples create files. Not a ByteMind CodeRunner example.
Formal Syntax & Structure
typescript syntaxtypescript
builder.getSemanticDiagnosticsOfNextAffectedFile(cancellationToken?: ts.CancellationToken, ignoreSourceFile?: (sourceFile: ts.SourceFile) => boolean): ts.AffectedFileResult<readonly ts.Diagnostic[]>Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| cancellationToken | ts.CancellationToken | Optional cancellation token. |
| ignoreSourceFile | (sourceFile: ts.SourceFile) => boolean | Optional skip predicate; use carefully. |
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 fs = require("node:fs");const path = require("node:path");function put(name, text) { fs.mkdirSync(path.dirname(name), { recursive: true }); fs.writeFileSync(name, text);}const options = { strict: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, types: [], incremental: true, declaration: true, outDir: "dist", tsBuildInfoFile: "cache/state.tsbuildinfo" };const root = path.resolve("main.ts");put(root, 'export const n: number = "bad";');const host = ts.createIncrementalCompilerHost(options);const b = ts.createSemanticDiagnosticsBuilderProgram([root], options, host);const errors = [];let item;while ((item = b.getSemanticDiagnosticsOfNextAffectedFile()) !== undefined) { if (item.affected === b.getSourceFile(root)) errors.push(...item.result.map(d=>d.code));}assert.deepEqual(errors, [2322]);assert.equal(b.getSemanticDiagnosticsOfNextAffectedFile(), undefined);console.log("affected main.ts: 2322; drained");
// Run: node example.cjs// Expected stdout: affected main.ts: 2322; drained