Control-flow graphs
Concept and boundary
The binder and checker cooperate on control-flow analysis; the checker can report different types at distinct use sites of the same symbol. Guards, assignments and reachability affect these facts. The public location query observes the resulting narrowing without relying on private flow-node layouts.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: getTypeAtFlowNode (line 73966). 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-type-at-location
Revisit reference fixture ts-api-get-type-at-location (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 checker = program.getTypeChecker();const source = program.getSourceFile("input.ts");const observed = [];function visit(node) { if (ts.isCallExpression(node) && node.expression.getText(source) === "take") { observed.push(checker.typeToString(checker.getTypeAtLocation(node.arguments[0]))); } ts.forEachChild(node, visit);}visit(source);assert.deepEqual(observed, ["string | number", "string"]);console.log(observed.join(" -> "));declare function take(value: unknown): void;function test(value: string | number) { take(value); if (typeof value === "string") take(value); }Expected stdout (exit 0):
string | number -> stringWorkshop 2: get-contextual-type
Revisit reference fixture ts-api-get-contextual-type (historical V28, rerun in V74).
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,find,variable}=inspect("const transform: (n:number)=>string = n=>String(n); const standalone=42;");const arrow=find(ts.isArrowFunction), context=c.getContextualType(arrow); assert(context);assert.equal(c.typeToString(context),"(n: number) => string");assert.equal(c.typeToString(c.getTypeAtLocation(arrow.parameters[0].name)),"number");assert.equal(c.getContextualType(variable("standalone").initializer),undefined);console.log("(n: number) => string; standalone: no context");Expected stdout (exit 0):
(n: number) => string; standalone: no contextPractice and recap
Assign a number inside a formerly string-narrowed branch and inspect the next use.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Control-flow graphs — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Control-flow graphs?
What does workshop 1 (get-type-at-location) 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 checker = program.getTypeChecker();const source = program.getSourceFile("input.ts");const observed = [];function visit(node) { if (ts.isCallExpression(node) && node.expression.getText(source) === "take") { observed.push(checker.typeToString(checker.getTypeAtLocation(node.arguments[0]))); } ts.forEachChild(node, visit);}visit(source);assert.deepEqual(observed, ["string | number", "string"]);console.log(observed.join(" -> "));What does workshop 2 (get-contextual-type) print when run unchanged with the specified compiler?
const ts = require("typescript");const assert = require("node:assert/strict");const fs = require("node:fs");const path = require("node:path");function inspect(text, extra = {}) { fs.writeFileSync("main.ts", text); for (const [name, content] of Object.entries(extra)) fs.writeFileSync(name, content); const program = ts.createProgram([path.resolve("main.ts")], { strict: true, types: [], target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, noEmit: true }); assert.deepEqual(ts.getPreEmitDiagnostics(program).map(d => d.code), []); const source = program.getSourceFile(path.resolve("main.ts")); const checker = program.getTypeChecker(); function find(predicate) { function visit(node) { return predicate(node) ? node : ts.forEachChild(node, visit); } const node = visit(source); assert(node); return node; } function variable(name) { return find(n => ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name); } return { source, checker, find, variable };}const {checker:c,find,variable}=inspect("const transform: (n:number)=>string = n=>String(n); const standalone=42;");const arrow=find(ts.isArrowFunction), context=c.getContextualType(arrow); assert(context);assert.equal(c.typeToString(context),"(n: number) => string");assert.equal(c.typeToString(c.getTypeAtLocation(arrow.parameters[0].name)),"number");assert.equal(c.getContextualType(variable("standalone").initializer),undefined);console.log("(n: number) => string; standalone: no context");