问题
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:
And escapedText ETypes references to an enum reference.
回答1:
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...
来源:https://stackoverflow.com/questions/45660003/find-kind-of-typereference-using-typescript-api