Binder and symbol tables
Concept and boundary
The binder associates declarations with symbols in scopes and helps establish flow information. The checker may merge declarations across files and exposes semantic identity via public queries. A symbol can have several declaration nodes; source order and scope matter. Do not depend on private locals tables in production tooling.
Pinned implementation breadcrumbs
In the installed TypeScript 5.8.3 bundle lib/typescript.js the following function definitions were located: bindSourceFile (line 46582); declareSymbol (line 46784). 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: symbol
Revisit reference fixture ts-api-symbol (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 first = source.statements[0];const second = source.statements[1];const a = checker.getSymbolAtLocation(first.name);const b = checker.getSymbolAtLocation(second.name);assert(a && a === b);assert.equal(a.getDeclarations().length, 2);console.log(a.getName(), a.getDeclarations().length);Expected stdout (exit 0):
Item 2Workshop 2: symbol-flags
Revisit reference fixture ts-api-symbol-flags (historical V27, 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}=inspect("import { item as local } from './dep'; const copy=local;",{"dep.ts":"export const item=7;"});const n=find(ts.isImportSpecifier).name,s=c.getSymbolAtLocation(n);assert(s.flags & ts.SymbolFlags.Alias); const target=c.getAliasedSymbol(s);assert(target.flags & ts.SymbolFlags.BlockScopedVariable); console.log("alias -> block-scoped variable");Expected stdout (exit 0):
alias -> block-scoped variablePractice and recap
Merge two interface declarations then compare against a distinct declaration in another module.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Binder and symbol tables — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes Binder and symbol tables?
What does workshop 1 (symbol) 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 first = source.statements[0];const second = source.statements[1];const a = checker.getSymbolAtLocation(first.name);const b = checker.getSymbolAtLocation(second.name);assert(a && a === b);assert.equal(a.getDeclarations().length, 2);console.log(a.getName(), a.getDeclarations().length);What does workshop 2 (symbol-flags) 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}=inspect("import { item as local } from './dep'; const copy=local;",{"dep.ts":"export const item=7;"});const n=find(ts.isImportSpecifier).name,s=c.getSymbolAtLocation(n);assert(s.flags & ts.SymbolFlags.Alias); const target=c.getAliasedSymbol(s);assert(target.flags & ts.SymbolFlags.BlockScopedVariable); console.log("alias -> block-scoped variable");