TypeScript Compiler API

getTypeOfSymbolAtLocation()

Gets the value-side type of a symbol in the context of a node. For class symbols this describes the constructor and static members rather than the declared instance shape. Use nodes and symbols from the current program; do not infer flow-sensitive facts for arbitrary detached nodes. Baseline: TypeScript 5.8.3 and Node.js. The example is a local tooling script in a disposable directory; it writes temporary project files, not a ByteMind CodeRunner exercise.

Formal Syntax & Structure

typescript syntaxtypescript
checker.getTypeOfSymbolAtLocation(symbol: ts.Symbol, node: ts.Node): ts.Type

Parameters & Attributes

NameTypeDescription
symbolts.SymbolValue symbol to inspect.
nodets.NodeContext node 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 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("class Box { count=1; static label='x'; }");const cls=find(ts.isClassDeclaration), s=c.getSymbolAtLocation(cls.name);const t=c.getTypeOfSymbolAtLocation(s,cls.name);assert(c.getPropertyOfType(t,"label")); assert.equal(c.getPropertyOfType(t,"count"),undefined);assert.equal(c.getSignaturesOfType(t,ts.SignatureKind.Construct).length,1);console.log("constructor: label, not count");
// Run: node example.cjs// Expected stdout: constructor: label, not count
TypeScript Reference