TypeScript Compiler API
TypeChecker.getSymbolAtLocation()
Returns the symbol associated with a suitable program node, often an identifier or a property name. A missing symbol is a valid result. Import names can resolve to alias symbols; use checker.getAliasedSymbol for an alias when the target entity is needed. Not every AST node has a symbol. 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
checker.getSymbolAtLocation(node: ts.Node): ts.Symbol | undefinedParameters & Attributes
| Name | Type | Description |
|---|---|---|
| node | ts.Node | Location to query in the current Program. |
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 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());
// File: input.ts (omit this label)import { value as local } from "./dep"; export const result = local;
// File: dep.ts (omit this label)export const value = 7;
// Run: node example.cjs// Expected stdout: local -> value
// Separate variation in a fresh directory.// 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 statement = source.statements[0];assert(ts.isExpressionStatement(statement));assert.equal(checker.getSymbolAtLocation(statement.expression), undefined);console.log("undefined");
// File: input.ts (omit this label)missingName;
// Run: node example.cjs// Expected stdout: undefined