title: TypeScript - passing a class as an argument, and reflection
body: <p>I am writing a generic unmarshaller. It converts graph DB data to generated TypeScript (1.8.7) model classes. The input is JSON. The output should be an instance of a model class.<br>
  <em>My ultimate goal is to create something like Hibernate OGM, only for Tinkerpop Frames and TypeScript, with REST endpoint in the middle.</em></p>
  
  <p><strong>What's the right way to pass a class as a parameter and reach it's static members?</strong> I want to have something like this:</p>
  
  <pre><code>SomeModel some = &lt;SomeModel&gt; unmarshaller.fromJSON({/*Object from JSON*/}, SomeModel);
  </code></pre>
  
  <p>I've tried to write a method.
  Not sure if I am heading in the right direction, feel free to suggest different approaches.</p>
  
  <pre><code>public fromJSON(input: Object, clazz: typeof FrameModel): FrameModel
  {
      // This only demonstrates access to Framemodel's metadata 
      // about original Java model classes.
      clazz.graphPropertyMapping;
      clazz.graphRelationMapping;
  
      let result = {};
      ...
      return result;
  }
  ...
  </code></pre>
  
  <p>But when I tried to execute this on Plunker, I got execution errors with unuseful stacktrace.</p>
  
  <p>The model superclass looks like this:</p>
  
  <pre><code>/**
   * Things common to all Frames models on the Typescript side.
   */
  export class FrameModel
  {
      // Model metadata
      static discriminator: string;
      static graphPropertyMapping: { [key:string]:string; };
      static graphRelationMapping: { [key:string]:string; };
  
      // Each instance needs a vertex ID
      private vertexId: number;
      public getVertexId(): number {
          return this.vertexId;
      }
  }
  </code></pre>
  
  <p>Sample model class:</p>
  
  <pre><code>import {TestPlanetModel} from './TestPlanetModel';
  import {TestShipModel} from './TestShipModel';
  
  export class TestGeneratorModel extends FrameModel
  {
      static discriminator: string = 'TestGenerator';
      static graphPropertyMapping: { [key:string]:string; } = {
          bar: 'boo',
          name: 'name',
          rank: 'rank',
      };
      static graphRelationMapping: { [key:string]:string; } = {
          colonizes: 'colonizedPlanet',
          commands: 'ship',
      };
  
      boo: string;
      name: string;
      rank: string;
  
      public colonizedPlanet: TestPlanetModel[]; // edge label 'colonizedPlanet'
  
      public ship: TestShipModel; // edge label 'ship'
  
  }
  </code></pre>
  
  <p>I haven't found much material on reflection and class handling in TypeScript.<br>
  I know how I would do this in Java.<br>
  I know how I would do this in JavaScript.<br>
  I understand that I might achieve similar results with decorators, but having fields or static fields seemed a bit simpler, for generated models.</p>