TypeScript Compiler API

ts.Symbol

A semantic entity known to the checker, such as a variable, member or interface. It is unrelated to JavaScript Symbol values. Multiple declarations can refer to the same symbol, as in declaration merging. Alias symbols may need getAliasedSymbol; handle undefined results and declarations without runtime values. 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
interface ts.Symbol; symbol.getName(); symbol.getDeclarations(): ts.Declaration[] | undefined

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 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);
// File: input.ts (omit this label)interface Item { id: number }interface Item { name: string }
// Run: node example.cjs// Expected stdout: Item 2
TypeScript Reference