title: Implementing an interface in Javascript using flow-runtime
body: <p>I am using <code>flow-runtime</code> plugin for <code>babel</code> to generate dynamically typechecked javascript code. The following is the workflow I am using</p>
  
  <ul>
  <li><p>write static javascript code (with flow annotations)</p></li>
  <li><p>compile this code using babel to convert flow annotations to typechecked code</p></li>
  <li><p>run this compiled code in node.js</p></li>
  </ul>
  
  <p>The following workflow gives me an ability to write typescript type code, but with type checking only where I want.</p>
  
  <p>So, now that we understand what I am doing, let me explain what I am trying to achieve</p>
  
  <p>I basically need to build a class called <code>Interface</code>, which will do exactly what it sounds like. This class will be extended by classes that are supposed to be interfaces, and then extended by other classes. Something like this : </p>
  
  <pre><code>class Interface() {
      constructor() {
          ...
      }
  
      // interface superclass, supposed to be extended by all interfaces
      // this class will provide all the utility methods required 
      // by an interface, such as validating the implementation of the 
      // interface, ...
  
      validateInterfaceImplementation() {
          ...
      }
  }
  
  // interface definition
  class FooInterface extends Interface {
      constructor() {
          super();
          ...
      }
  }
  
  // actual class, that will implement the "FooInterface" interface
  class Foo extends FooInterface {
      constructor() {
          super();
          ...
      }
  }
  </code></pre>
  
  <p>Now, I want to enforce strict implementation of the <code>FooInterface</code>. That means that I want a way to define all the methods that the <code>FooInterface interface</code> expects to be implemented, and validation that all these methods have been implemented by the <code>Foo</code> class.</p>
  
  <p>What I have tried looks something like this</p>
  
  <pre><code>// interface.js
  // @flow-runtime
  class Interface&lt;T&gt; {
      constructor(t: T) {
          (this: T); // let flow validate that the interface is implemented
      }
  }
  
  // FooInterface.js
  // @flow-runtime
  type foInterface = {
      bar(param: string): number;
  }
  
  class FooInterface extends Interface&lt;fooInterface&gt; {
      constructor() {
          super(fooInterface);
      }
  }
  
  // Foo.js
  // @flow-runtime
  class Foo extends FooInterface {
  
  }
  
  new Foo(); // should throw an error, because the interface is not implemented
             // (the function bar is not defined)
  </code></pre>
  
  <p>I am facing multiple problems with this approach</p>
  
  <ul>
  <li>I am not sure how to implement the generic class <code>Interface&lt;T&gt;</code>. In think my implementation is incorrect, and the compiled babel code also throws an error, but I can't figure out how to do this.</li>
  <li>I am not even sure whether this method will work or not, or whether this is the best way to approach this problem.</li>
  </ul>
  
  <p>Any help would be welcome. Thanks in advance :)</p>