使用 TypeScript 查找 TypeReference 的种类 API

Find kind of TypeReference using TypeScript API

我正在尝试查找 TypeReference.

的种类(class、接口、类型别名、枚举...)

我有这个:

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[]; 

但是调用getSymbolAtLocationreturnsundefined.

根据 VSC 调试器,

anodeTypeReferenceNode(种类 159):

并且 escapedText ETypes 引用枚举引用。

我找到了解决方案:

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[];

decls 数组中您可以找出声明是否为接口、类 等...