Israel
Israel

Reputation: 495

Typescript string prototype show error in webstorm IDE

I defined the following format function to String. This works OK, but the problem is that webstorm is marking the "String.prototype.format" in red. How can I suppress the error?

interface String {
    format(variables:Array<string>):string
}

String.prototype.format = (variables:Array<string>):string => {
    return this.replace(/%(\d+)/g, function(_,m) {
        return variables[--m];
    });
};

Thanks

Upvotes: 2

Views: 278

Answers (2)

anstarovoyt
anstarovoyt

Reputation: 8136

It is a bug. See WEB-14302

The problem should be fixed in the next WebStorm 11 EAP.

Upvotes: 0

Israel
Israel

Reputation: 495

I found a workaround for this, I ended up with this:

interface String {
    format(variables:Array<string>):string
}

if (!String.hasOwnProperty("format")) {
    String.prototype["format"] = function (variables:Array<string>) : string   {
        return this.replace(/%(\d+)/g, function(_,m) {
            return variables[--m];
        });
    };
}

Upvotes: 1

Related Questions