Find kind of TypeReference using TypeScript API

孤街浪徒 提交于 2020-05-26 06:11:09

问题


I'm trying to find the kind (class, interface, type alias, enumeration ...) of a TypeReference.

I have this:

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

But the call to getSymbolAtLocation returns undefined.

anode is a TypeReferenceNode (kind 159) according to VSC debugger:

And escapedText ETypes references to an enum reference.


回答1:


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

From the decls array you can find out whether the declarations are Interfaces, Classes, etc...



来源:https://stackoverflow.com/questions/45660003/find-kind-of-typereference-using-typescript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!