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.

chrisber
  • 730
  • 1
  • 12
  • 27

1 Answers1

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.

chrisber
  • 730
  • 1
  • 12
  • 27
  • 1
    https://atom.io/packages/atom-typescript does those tool tips just fine using lasagne service "getQuickInfo" – basarat Feb 24 '15 at 20:54
  • 2
    Also you can get the program from the language service method 'getProgram' – basarat Feb 24 '15 at 20:55
  • 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? – chrisber Feb 24 '15 at 21:52
  • 1
    Just langaugeServiceHost – basarat Feb 24 '15 at 22:33
  • 1
    Did a quick check. `getProgram` is coming in TS version 1.5. Not there in 1.4 yet. – basarat Feb 24 '15 at 22:34
  • I don't think it works anymore given I think getNamedDeclarations was removed – Benjamin Gruenbaum Aug 17 '21 at 08:35