I want to add extension method format()
to String
. So my expectation is that I can use String.format
wherever in my project.
I had followed the guideline of this topic but this not help. I got this error:
Can anyone help me?
Thanks in advance.
p.s: I want to add the extension method like I did in angular 1.xx
Edit
use declare global
won't get error.
declare global {
interface String {
format(): string;
}}
String.prototype.format = function () :string {
var result = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
result = result.replace(reg, arguments[i + 1]);
}
return result;}
How we use String.format('<img alt="{0}" title="{0}" src="{1}" />', name, id);
Since format
does not require parameters