title: Extending instance/static functions on existing prototypes with TypeScript
body: <p>I recently asked a question about TypeScript's ability to extend existing prototypes in the JavaScript API (here: <a href="https://stackoverflow.com/questions/16813118/extending-object-prototype-with-typescript">Extending Object.prototype with TypeScript</a>).</p>
  
  <p>This turned out to be a bug, which has since been resolved as of TypeScript 0.9.0 Alpha (which now includes generics...GREAT :-))</p>
  
  <p>In TypeScript, interfaces are open ended, so if you look in <em>lib.d.ts</em>, you will find an interface which defines the contract for JavaScript's Object API. You should also see a variable declaration for Object, which defines Object's static functions.</p>
  
  <p>For the sake of simplicity, here they are:</p>
  
  <pre><code>//Pulled from lib.d.ts
  
  interface Object {
      toString(): string;
      toLocaleString(): string;
      valueOf(): Object;
      hasOwnProperty(v: string): bool;
      isPrototypeOf(v: Object): bool;
      propertyIsEnumerable(v: string): bool;
      [s: string]: any;
  }
  
  declare var Object: {
      new (value?: any): Object;
      (): any;
      (value: any): any;
      prototype: Object;
      getPrototypeOf(o: any): any;
      getOwnPropertyDescriptor(o: any, p: string): PropertyDescriptor;
      getOwnPropertyNames(o: any): string[];
      create(o: any, properties?: PropertyDescriptorMap): any;
      defineProperty(o: any, p: string, attributes: PropertyDescriptor): any;
      defineProperties(o: any, properties: PropertyDescriptorMap): any;
      seal(o: any): any;
      freeze(o: any): any;
      preventExtensions(o: any): any;
      isSealed(o: any): bool;
      isFrozen(o: any): bool;
      isExtensible(o: any): bool;
      keys(o: any): string[];
  }
  </code></pre>
  
  <p>So, my previous question was about extending the interface for Object, like so:</p>
  
  <pre><code>interface Object {
      GetFoo(): Foo;
  }
  
  Object.prototype.GetFoo = function() {
      return new Foo();
  }
  </code></pre>
  
  <p>As stated, this works as of TypeScript 0.9.0.</p>
  
  <p>But now I also want to be able to add static functions to Object, perfectly legal in pure JavaScript.</p>
  
  <p>In JavaScript I would do:</p>
  
  <pre><code>Object.FooAgain = function () {
      // Yes, it's foo again!
  }
  </code></pre>
  
  <p>But I can't seem to be able to accomplish this in TypeScript.</p>
  
  <p>First I tried to see if Object's declaration was open ended (as with interfaces):</p>
  
  <pre><code>declare var Object: {
      FooAgain(): void;
  }
  
  Object.FooAgain = function () {
      // TS doesn't like this, and Object.FooAgain isn't available in intellisense.
  }
  </code></pre>
  
  <p>I also tried (I saw this in another article, and thought it was worth a try).</p>
  
  <pre><code>export declare var Object: {
  }
  </code></pre>
  
  <p>but this seems to break everything even more...</p>
  
  <p>Admittedly this issue may have also been fixed in TS 0.9.0 (I'm at work so I haven't actually tried it fully).</p>
  
  <p>Can anyone shed some light on this?</p>