使用 TypeScript API 查找类型引用 [英] Find kind of TypeReference using TypeScript API
本文介绍了使用 TypeScript API 查找类型引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找 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
是 TypeReferenceNode
(种类 159):
anode
is a TypeReferenceNode
(kind 159) according to VSC debugger:
和转义文本 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 查找类型引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文