1

Let's say I want to add the following prototype to the String class.

String.prototype.beginsWith = function (string) {
     return(this.indexOf(string) === 0);
};

I need to add beginWith to lib.d.ts otherwise it won't complile:

declare var String: {
    new (value?: any): String;
    (value?: any): string;
    prototype: String;
    fromCharCode(...codes: number[]): string;
    //Here
}

The file is locked and I can't edit it.

I release I can just declare var String: any before the call but can I have it built in?

1 Answer 1

6

You don't need to modify the lib.d.ts instead extend the String interface first, then include the new method to the prototype chain of the object you wish to extend.

For e.g.

interface String {
   beginsWith(text: string): bool;
}

Then implement the new functionality and add it to the prototype chain

String.prototype.beginsWith = function (text) {
    return this.indexOf(text) === 0;
}

Now you will get the intellisense in the calling code and works as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.