3

I'm trying to find the kind (class, interface, type alias, enumeration ...) of a TypeReference.

I have this:

const anode = node as ts.TypeReferenceNode;
const symbol = this.typechecker.getSymbolAtLocation(anode.typeName) as ts.Symbol;
const type = this.typechecker.getTypeOfSymbolAtLocation(symbol, anode);
const decls = symbol.getDeclarations() as ts.Declaration[]; 

But the call to getSymbolAtLocation returns undefined.

anode is a TypeReferenceNode (kind 159) according to VSC debugger:

enter image description here

And escapedText ETypes references to an enum reference.

1 Answer 1

7

I found out a solution:

const anode = node as ts.TypeReferenceNode;
const type = this.typechecker.getTypeAtLocation(anode);
const symbol = type.symbol || type.aliasSymbol;
const decls = symbol.getDeclarations() as ts.Declaration[];

From the decls array you can find out whether the declarations are Interfaces, Classes, etc...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.