How to get property info by using the Typescript language service

☆樱花仙子☆ 提交于 2020-12-05 17:23:19

问题


How to get the type of a property of an object with Typescript 1.4.0.

I'm searching for something similar to C# it has the possibility to look up the properties of an object.

var properties = typeof(T).GetProperties();
foreach( var property in properties){}

What I have so far is:

var ls = ts.createLanguageService(host, ts.createDocumentRegistry())
var nav = ls.getNavigationBarItems(host.fileName);

Given the example Interface:

interface Example {
    firstname: string;
    lastname: string;
    age: string;
}

The TypeScript language service returns the result:

{  
   "NavigationBarItems":[  
      {  
         "text":"Example",
         "kind":"interface",
         "kindModifiers":"",
         "spans":[  
            {  
               "start":0,
               "length":83
            }
         ],
         "childItems":[  
            {  
               "text":"age",
               "kind":"property",
               "kindModifiers":"",
               "spans":[  
                  {  
                     "start":69,
                     "length":12
                  }
               ],
               "childItems":[  

               ],
               "indent":0,
               "bolded":false,
               "grayed":false
            },
            {  
               "text":"firstname",
               "kind":"property",
               "kindModifiers":"",
               "spans":[  
                  {  
                     "start":24,
                     "length":18
                  }
               ],
               "childItems":[  

               ],
               "indent":0,
               "bolded":false,
               "grayed":false
            },
            {  
               "text":"lastname",
               "kind":"property",
               "kindModifiers":"",
               "spans":[  
                  {  
                     "start":47,
                     "length":17
                  }
               ],
               "childItems":[  

               ],
               "indent":0,
               "bolded":false,
               "grayed":false
            }
         ],
         "indent":0,
         "bolded":false,
         "grayed":false
      }
   ]
}

The information I'm missing is the type (string,number,Map<>,any) and if it is an array or object e.g.

"text":"lastname",
"kind":"property",
"type":"string",  //string,number,Map<>,any

Any Idea how to achieve this?

Your help is highly appreciated.


回答1:


Found the solution, don't know how I missed it in the first place. Nicholas Wolverson has an excelent blog post about it. Using the TypeScript language service was not the correct choice. The correct solution is to use the TypeScript compiler API.

var program = ts.createProgram([dummyScriptName], host.getCompilationSettings(), host);
var typeChecker = program.getTypeChecker(true);
var sf = program.getSourceFile(dummyScriptName);
var decls = sf.getNamedDeclarations().map(function (nd) { return nd.symbol.name + ": " + typeChecker.typeToString(typeChecker.getTypeAtLocation(nd)); });

This will return the needed information

interface Person {
    firstname: string;
    lastname: string;
    age: number[];
}

result:

Person: Person
firstname: string
lastname: string
age: number[]

please use (code, mirror_code, blog post) for details.



来源:https://stackoverflow.com/questions/28692515/how-to-get-property-info-by-using-the-typescript-language-service

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