title: Extending a prototype from an external library in Typescript
body: <p>I would like to extend the prototype of an external library in Typescript (1.8). The external library in my case is google.maps for which I have included the Typescript Definition file from <a href="https://www.nuget.org/packages/google.maps.d.ts" rel="nofollow noreferrer">NuGet</a>. Now I want to extend the prototype of the google.maps.LatLng by adding a distance function like so in a file "extensions.ts":</p>
  
  <pre><code>/// &lt;reference path="./google.maps.d.ts" /&gt;
  export {}
  
  declare global {
      interface google.maps.LatLng {
          distanceTo(google.maps.LatLng): number;
      }
  }
  
  google.maps.LatLng.prototype.distanceTo = function(other) {
      return google.maps.geometry.spherical.computeDistanceBetween(this, other);
  }
  </code></pre>
  
  <p>Then I want to use it:</p>
  
  <pre><code>import "./extensions.ts"
  
  let p1 = new google.maps.LatLng(5, 5);
  let p2 = new google.maps.LatLng(10, 10);
  
  console.log(p1.distanceTo(p2))
  </code></pre>
  
  <p>However this code does not work with many errors :/
  What is the correct way of solving that? Extending the global declarations like Array or String works that way as mentioned <a href="https://stackoverflow.com/questions/38878403/add-a-function-to-a-prototype-globally-from-within-a-module/38880585#38880585">here</a>.</p>
  
  <p>Note: For the resulting file to work you also have to include the google maps javascript library:</p>
  
  <pre><code>&lt;script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry&amp;key=[your-key-here]&amp;sensor=FALSE"&gt;&lt;/script&gt;
  </code></pre>