我正在尝试查找 TypeReference
的种类(类、接口(interface)、类型别名、枚举...)。
我有这个:
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[];
但是对 getSymbolAtLocation
的调用返回 undefined
。
anode
是一个 TypeReferenceNode
(种类 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
数组中,您可以找出声明是否为接口(interface)、类等...
关于typescript - 使用 TypeScript API 查找类型引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45660003/