使用TypeScript API查找TypeReference的种类 [英] Find kind of TypeReference using TypeScript API
本文介绍了使用TypeScript API查找TypeReference的种类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找 TypeReference
的类型(类,接口,类型别名,枚举...)。
I'm trying to find the kind (class, interface, type alias, enumeration ...) of a TypeReference
.
我有这个:
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
。
But the call to getSymbolAtLocation
returns undefined
.
anode
是根据VSC调试器的 TypeReferenceNode
(类型159):
anode
is a TypeReferenceNode
(kind 159) according to VSC debugger:
和对枚举引用的escapedText ETypes
引用。
And escapedText ETypes
references to an enum reference.
推荐答案
我找到了一个解决方案:
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[];
从 decls
数组中可以找到声明是否为接口,类等...
From the decls
array you can find out whether the declarations are Interfaces, Classes, etc...
这篇关于使用TypeScript API查找TypeReference的种类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文