【问题标题】:Find kind of TypeReference using TypeScript API使用 TypeScript API 查找类型引用
【发布时间】:2017-08-14 19:12:24
【问题描述】:

我正在尝试查找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

根据 VSC 调试器,anodeTypeReferenceNode(种类 159):

并且 escapedText ETypes 引用了一个枚举引用。

【问题讨论】:

    标签: typescript abstract-syntax-tree


    【解决方案1】:

    我找到了解决办法:

    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 数组中,您可以找出声明是接口、类等...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 2021-03-29
      • 2021-02-21
      • 2020-05-16
      • 2018-01-16
      • 2021-12-30
      • 2021-05-12
      相关资源
      最近更新 更多