title: Can you dynamically extend a type in typescript?
body: <p>In javascript you can do this:</p>
  
  <pre><code>function Test() {
    this.id = 1;
  };
  
  Test.prototype.customize = function(key, callback) {
    this[key] = callback;
  };
  
  var callback = function() { alert(this.id); };
  var x = new Test();
  x.customize('testing', callback);
  x.testing();
  </code></pre>
  
  <p>Can you do a similar thing in typescript?</p>
  
  <p>Particularly I'm interested in having a class like:</p>
  
  <pre><code>class Socket {
    ...
  }
  
  class Sockets {
  
    public addChannel(name:string):void {
      this[name] = new Socket(); 
    }
  
    ...
  }
  
  data = new Sockets();
  data.addChannel('video');
  data.addChannel('audio');
  
  ...
  
  var audio = data.audio.read();
  var video = data.video.read();
  etc.
  </code></pre>
  
  <p>The compiler complains that there's no 'audio' or 'video' member on 'Sockets', and won't compile. Is there a way to work around that without having to manually define the properties on the container class?</p>
  
  <p>I know it kind of side steps the static typing rules, but I find it occasionally useful for API niceness to have something like this.</p>
  
  <p>edit: See the example answer I posted below; something like that works. I'll still accept any clever answer that lets me somehow manage to compile something that does something useful on the base object itself.</p>