title: Polyfills, Shims & Extensions with TypeScript
body: <p>Before I proceed with my question, allow me to point out that I have already done some considerable research into this topic, and have already asked a couple of related questions, which you can find here:</p>
  
  <p><a href="https://stackoverflow.com/questions/16813118/extending-object-prototype-with-typescript">Extending Object.prototype with TypeScript</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16816414/extending-instance-static-functions-on-existing-prototypes-with-typescript">Extending instance/static functions on existing prototypes with TypeScript</a></p>
  
  <p>Whilst TypeScript seems to be maturing quite well along it's road to version 1.0, I'd noticed quite early on, in it's infancy that it was not easy to polyfill/shim/monkey patch/extend the functionality of core objects (Object, String, Number, Boolean etc) using TypeScript...however this is perfectly legal in pure JavaScript, and in my opinion, necessary for creating cross browser compatible and compliant APIs.</p>
  
  <p>Now that we are on version 0.9.5 I've noticed much improvement in this area, and we can now extend the prototype of core objects, and it would seem that we can also add static functions to core objects too...HOWEVER...this appears to be buggy!</p>
  
  <p>Consider the following code example:</p>
  
  <pre><code>interface Object {
      /********************************/
      /*  Static functions            */
      /********************************/
  
      // ECMAScript 6 Object.is function
      // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
      is(v1: any, v2: any): boolean;
  
      /********************************/
      /*  Prototype functions         */
      /********************************/
  
      // Custom method for testing obejct equality (based on Java / C# principles)
      equals(obj: any): boolean;
  }
  
  // Object.is polyfill
  (() =&gt; {
      if(!Object.is) {
          Object.is = function(v1: any, v2: any): boolean {
              if (v1 === 0 &amp;&amp; v2 === 0) {
                  return 1 / v1 === 1 / v2;
              }
              if (v1 !== v1) {
                  return v2 !== v2;
              }
              return v1 === v2;
          }
      }
  })();
  
  // Object.prototype.equals implementation
  Object.prototype.equals = function(obj: any): boolean {
      return Object.is(obj, this);
  }
  </code></pre>
  
  <p>Wanna play with it?...<a href="http://www.typescriptlang.org/Playground/#src=interface%20Object%20%7B%0A%09/********************************/%0A%09/*%20%20Static%20functions%20%20%20%20%20%20%20%20%20%20%20%20*/%0A%09/********************************/%0A%09%0A%09//%20ECMAScript%206%20Object.is%20function%0A%09//%20https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is%0A%09is%28v1:%20any,%20v2:%20any%29:%20boolean;%0A%09%0A%09/********************************/%0A%09/*%20%20Prototype%20functions%20%20%20%20%20%20%20%20%20*/%0A%09/********************************/%0A%09%0A%09//%20Custom%20method%20for%20testing%20obejct%20equality%20%28based%20on%20Java%20/%20C#%20principles%29%0A%09equals%28obj:%20any%29:%20boolean;%0A%7D%0A%0A//%20Object.is%20polyfill%0A%28%28%29%20=%3E%20%7B%0A%09if%28!Object.is%29%20%7B%0A%09%09Object.is%20=%20function%28v1:%20any,%20v2:%20any%29:%20boolean%20%7B%0A%09%09%09if%20%28v1%20===%200%20&amp;&amp;%20v2%20===%200%29%20%7B%0A%09%09%09%09return%201%20/%20v1%20===%201%20/%20v2;%0A%09%09%09%7D%0A%09%09%09if%20%28v1%20!==%20v1%29%20%7B%0A%09%09%09%09return%20v2%20!==%20v2;%0A%09%09%09%7D%0A%09%09%09return%20v1%20===%20v2;%0A%09%09%7D%0A%09%7D%0A%7D%29%28%29;%0A%0A//%20Object.prototype.equals%20implementation%0AObject.prototype.equals%20=%20function%28obj:%20any%29:%20boolean%20%7B%0A%09return%20Object.is%28obj,%20this%29;%0A%7D" rel="nofollow noreferrer">Playground</a></p>
  
  <p>You've probably already noticed what's wrong here...interfaces shouldn't contain definitions for static functions, only those which should be bound to the prototype. In this respect, if you check out <a href="https://github.com/c9/typescript/blob/master/typings/lib.d.ts" rel="nofollow noreferrer">lib.d.ts</a>, you will see that static functions are declared differently:</p>
  
  <pre><code>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 that means that we should really be adding <code>is(v1: any, v2: any): boolean;</code> to the declaration...but declarations in TypeScript are not open-ended as are interfaces, therefore this is not possible.</p>
  
  <p>That being said, if I remove the declaration from the interface, TypeScript then complains that <code>Object.is</code> does not exist...so it would appear that I HAVE TO have it in there for the polyfill to work!</p>
  
  <p>"Well if it works...kinda what's the problem?" - Yes it works, but only just and it's not elegant by any means.</p>
  
  <p>Because is has been implemented statically, yet it is contracted as part of Object, TypeScript intellisense is still expecting <code>Object.prototype.is</code> AND does not recognize the existence of <code>Object.is</code></p>
  
  <p>I feel like I'm waffling now so I'll get to the point.</p>
  
  <ol>
  <li>Is this a known issue?</li>
  <li>have I missed something?</li>
  <li>What is in the pipeline to rectify this issue?</li>
  </ol>
  
  <p><strong>NOTE:</strong> I've just noticed, it appears that you can overwrite declarations now, so you could add <code>is(v1: any, v2: any): boolean;</code> to the declaration by overwriting it completely...but I still feel that this is not the most elegant solution.</p>