title: Extending functionality in TypeScript
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12766117/how-does-prototype-extend-on-typescript">How does prototype extend on typescript?</a>  </p>
  </blockquote>
  
  
  
  <p>I am currently learning TypeScript, and would like to know how it is possible to add functionality to existing objects. Say I want to add an implementation for Foo to the String object. In JavaScript I would do this:</p>
  
  <pre><code>String.prototype.Foo = function() {
      // DO THIS...
  }
  </code></pre>
  
  <p>Understanding that TypeScript classes, interfaces and modules are open ended led me to try the following, without success</p>
  
  <p><em>1. Reference the JavaScript implementation from TypeScript</em></p>
  
  <pre><code>    JavaScript:
  
      String.prototype.Foo = function() {
          // DO THIS...
      }
  
      TypeScript:
  
      var x = "Hello World";
      x.Foo(); //ERROR, Method does not exist
  </code></pre>
  
  <p><em>2. Extend the interface</em></p>
  
  <pre><code>interface String {
      Foo(): number;
  }
  
  var x = "Hello World";
  x.Foo(); //Exists but no implementation.
  </code></pre>
  
  <p><em>3. Extend the class</em></p>
  
  <pre><code>class String {
      Foo(): number {
          return 0;
      }
  }
  
  // ERROR: Duplicate identifier 'String'
  </code></pre>
  
  <p>As you can see from these results, so far I have been able to add the method via an interface contract, but no implementation, so, how do I go about defining AND implementing my Foo method as part of the pre-existing String class?</p>