title: Get intance's class in TypeScript
body: <p>How to get the class of an instance in TypeScript? This question is similar to <a href="https://stackoverflow.com/questions/13613524/get-an-objects-class-name-at-runtime-in-typescript">Get an object&#39;s class name at runtime in TypeScript</a> but I don't want to get the class name. I want to get the class itself.</p>
  
  <p>What I need this for? TypeScript does not have class methods, only static methods. But they are not virtual. In order to fully translate my mental model into Js code, I need to have virtual class methods. </p>
  
  <p>My idea is to do something like this:</p>
  
  <pre><code>function classOf&lt;T&gt;(o: T) : any {
      return o.class; // How to write this???
  }
  
  class Foo {
      static tryMe() : string {
          return "foo";
      }
  
  }
  
  class Bar extends Foo {
      static tryMe() : string {
          return "bar";
      }
  
  }
  
  function testPolymorphClassMethod(instance : Foo) {
      console.log(classOf(instance).tryMe());
  }
  
  testPolymorphClassMethod(new Foo()); // Should print "foo"
  testPolymorphClassMethod(new Bar()); // Should print "bar"
  </code></pre>
  
  <p>Whenever a concrete class is available, I could call its static method directly. If only an instance is available, I would like to call the static method of its class, e.g. making it virtual by hand.</p>
  
  <p>The problem is that I have no idea how to write the classOf function. It may not be possible at all.</p>
  
  <p>I was also trying this:</p>
  
  <pre><code>function classOf&lt;T&gt;(o: T) : typeof T {
      return typeof o;
  }
  </code></pre>
  
  <p>But it gives a comiple error:</p>
  
  <p>"T only refers to a type, but it is used as value here".</p>
  
  <p>I know that functions are first class objects in JavaScript (and also TypeScript). But TypeScript classes may not be, and then maybe it will be impossible to do what I want.</p>
  
  <p>Is there a workaround for this that does not require typing lots of code for each and every descendant class?</p>
  
  <p>The closest thing I could get is:</p>
  
  <pre><code>class Virtualizer {
      virtual(methodName: string) : any {
          return eval(this.constructor.name+"."+methodName);
      }
  }
  
  class Foo extends Virtualizer {
      static getMyName() : string {
          return "foo";
      }
  
      public test() : string {
          return this.virtual("getMyName")();
      }
  
  }
  
  class Bar extends Foo {
      static getMyName() : string {
          return "bar";
      }    
  }
  
  let foo = new Foo();
  let bar = new Bar();
  
  console.log(foo.test()); // Prints "foo"
  console.log(bar.test()); // Prints "bar"
  </code></pre>
  
  <p>But this looses all type information, it needs a common base class (or code repetition), and I can never call the actual method that I want to call, just a "virtualizer". It is so clumsy.</p>
  
  <p>I could extract the class related information into an encapsulated singleton object, but that will require me to change visibility of many methods from protected to public. (And also generate lots of cross references between the main and the encapsulated objects, very clumsy too.)</p>