title: Dynamic class methods
body: <p>Does anyone have a good fix on getting type checking when extending a class with dynamic methods? For example say you want to extend the class with methods based on options passed to the constructor. This is common in plain ol' JavaScript.</p>
  
  <pre><code>const defaults = {
    dynamicMethods: ['method1', 'method2'];
  };
  
  class Hello {
    constructor(options) {
      options.dynamicMethods.forEach(m =&gt; this[m] = this.common);
    }
    private common(...args: any[]) {
       // do something.
    }
  }
  
  const hello = new Hello(defaults);
  </code></pre>
  
  <p>Of course the above will work and you'll be able to call these dynamic methods but you won't get intellisense. </p>
  
  <p>Not you can solve this problem with something like the below:</p>
  
  <pre><code>class Hello&lt;T&gt; {
    constructor(options) {
      options.dynamicMethods.forEach(m =&gt; this[m] = this.common);
    }
    private common(...args: any[]) {
       // do something.
    }
  }
  
  interface IMethods {
    method1(...args: any[]);
    method2(...args: any[]);
  }
  
  function Factory&lt;T&gt;(options?): T &amp; Hello&lt;T&gt; {
    const hello = new Hello&lt;T&gt;(options);
    return hello as T &amp; Hello&lt;T&gt;;
  }
  </code></pre>
  
  <p>To consume this:</p>
  
  <pre><code>import { Factory } from './some/path'
  const hello = new Factory&lt;IMethods&gt;(defaults);
  </code></pre>
  
  <p>This of course works but wondering what other alternatives exist!</p>