Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:46:00+00:00 2026-06-16T02:46:00+00:00

Possible Duplicate: How does prototype extend on typescript? I am currently learning TypeScript, and

  • 0

Possible Duplicate:
How does prototype extend on typescript?

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:

String.prototype.Foo = function() {
    // DO THIS...
}

Understanding that TypeScript classes, interfaces and modules are open ended led me to try the following, without success

1. Reference the JavaScript implementation from TypeScript

    JavaScript:

    String.prototype.Foo = function() {
        // DO THIS...
    }

    TypeScript:

    var x = "Hello World";
    x.Foo(); //ERROR, Method does not exist

2. Extend the interface

interface String {
    Foo(): number;
}

var x = "Hello World";
x.Foo(); //Exists but no implementation.

3. Extend the class

class String {
    Foo(): number {
        return 0;
    }
}

// ERROR: Duplicate identifier 'String'

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:46:02+00:00Added an answer on June 16, 2026 at 2:46 am

    I have found the solution. It takes a combination of the interface and the JavaScript implementation. The interface provides the contract for TypeScript, allowing visibility of the new method. The JavaScript implementation provides the code that will be executed when the method is called.

    Example:

    interface String {
        foo(): number;
    }
    
    String.prototype.foo= function() {
        return 0;
    }
    

    As of TypeScript 1.4 you can now also extend static members:

    interface StringConstructor {
        bar(msg: string): void;
    }
    
    String.bar = function(msg: string) {
        console.log("Example of static extension: " + msg);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What does PHP keyword 'var' do? I know this can sound like
Possible Duplicate: Does every thread need its own autorelease pool? I would like to
Possible Duplicate: What does a CALLBACK declaration in C do? The prototype of a
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know
Possible Duplicate: IE only javascript error with getElementsByTagName var rows=myTable.getElementsByTagName('tr'); rows=Array.prototype.slice.call(rows,0); Does not work
Possible Duplicate: Does anyone have a good Proper Case algorithm I am currently in
Possible Duplicate: Does HTML5 require clients to support JavaScript? Would HTML5 API's work when
Possible Duplicate: Does the Facebook 'like' have a limit? Facebook like button updates the
Possible Duplicate: How does JavaScript .prototype work? What is the use of prototype property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.