How to get property info by using the Typescript language service
Asked Answered
A

1

4

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.

Arsenal answered 24/2, 2015 at 9:47 Comment(0)
A
3

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();
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.

Arsenal answered 24/2, 2015 at 19:16 Comment(6)
atom.io/packages/atom-typescript does those tool tips just fine using lasagne service "getQuickInfo"Taipan
Also you can get the program from the language service method 'getProgram'Taipan
Your are absolutely right. I can use getQuickInfoAtPosition to get the info I need but its a little bit uncomfortable to lookup every item at its position. Thanks for the note for getProgram, do you know if I need to implement the compiler host when I want to use getProgram? Or can I stay with languageservicehost?Arsenal
Just langaugeServiceHostTaipan
Did a quick check. getProgram is coming in TS version 1.5. Not there in 1.4 yet.Taipan
I don't think it works anymore given I think getNamedDeclarations was removedNonmetal

© 2022 - 2024 — McMap. All rights reserved.