TypeChecker and symbols
Concept and boundary
A compiler Symbol represents a semantic declaration identity, not the JavaScript Symbol primitive. Merged declarations can share a symbol; imported aliases can have a separate symbol from their target. Check for an absent result before reading declarations.
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: get-symbol-at-location
Revisit reference fixture ts-api-get-symbol-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 specifier = source.statements[0].importClause.namedBindings.elements[0];const symbol = checker.getSymbolAtLocation(specifier.name);assert(symbol && (symbol.flags & ts.SymbolFlags.Alias));const target = checker.getAliasedSymbol(symbol);assert.equal(target.getName(), "value");console.log(symbol.getName(), "->", target.getName());import { value as local } from "./dep"; export const result = local;Expected stdout (exit 0):
local -> valuePractice and recap
Compare symbol identity at a declaration and two uses, then add a shadowing declaration.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TypeChecker and symbols — knowledge check
Check the concept and interpret the locally verified compiler tooling probes.
Which statement correctly describes TypeChecker and symbols?
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 (get-symbol-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 specifier = source.statements[0].importClause.namedBindings.elements[0];const symbol = checker.getSymbolAtLocation(specifier.name);assert(symbol && (symbol.flags & ts.SymbolFlags.Alias));const target = checker.getAliasedSymbol(symbol);assert.equal(target.getName(), "value");console.log(symbol.getName(), "->", target.getName());