Typescript String.format不存在
我有一个字符串常量,必须替换两个单词,如下所示:Typescript String.format不存在,typescript,string-interpolation,Typescript,String Interpolation,我有一个字符串常量,必须替换两个单词,如下所示: public static readonly MY_STRING: string = 'page={0}&id={1}'; if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number)
public static readonly MY_STRING: string = 'page={0}&id={1}';
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
MY_STRING.format(page, id)
function stringFormat(template: string, ...args: any[]) {
return template.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
const myString: string = 'page={0}&id={1}';
const formattedWithFormat = stringFormat(myString, 123, 456);
console.log(formattedWithFormat); // page=123&id=456
必须用其他字符串替换0和1。我在不同的答案中读到了String.format,他们建议提供如下实现:
public static readonly MY_STRING: string = 'page={0}&id={1}';
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
MY_STRING.format(page, id)
function stringFormat(template: string, ...args: any[]) {
return template.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
const myString: string = 'page={0}&id={1}';
const formattedWithFormat = stringFormat(myString, 123, 456);
console.log(formattedWithFormat); // page=123&id=456
但是当我执行String.format
时,它会告诉我
Property 'format' does not exist on type 'String'
在这种情况下,使用字符串插值/替换的正确方法是什么?对于格式,我会这样做:
public static readonly MY_STRING: string = 'page={0}&id={1}';
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
MY_STRING.format(page, id)
function stringFormat(template: string, ...args: any[]) {
return template.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
const myString: string = 'page={0}&id={1}';
const formattedWithFormat = stringFormat(myString, 123, 456);
console.log(formattedWithFormat); // page=123&id=456
如何实现这一点?您可以从基本库中增加字符串声明:
declare global {
interface String {
format(...args: []): string
}
}
注意:如果不是在模块中,声明全局
是不必要的,您只需将接口字符串{…}
移动到顶层()可以考虑修改本机原型,如字符串
。由于JavaScript中的字符串没有标准或约定的format()
方法,因此添加自己的方法可能会导致在同一运行时运行的任何代码出现意外行为。您的实现甚至会首先检查现有的String.prototype.format
,这意味着如果有人首先使用不同的实现到达那里,那么您可能就是具有意外行为的人
使用stringFormat
函数完全没有问题,如下所示:
public static readonly MY_STRING: string = 'page={0}&id={1}';
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
MY_STRING.format(page, id)
function stringFormat(template: string, ...args: any[]) {
return template.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
const myString: string = 'page={0}&id={1}';
const formattedWithFormat = stringFormat(myString, 123, 456);
console.log(formattedWithFormat); // page=123&id=456
另外,JavaScript提供了基本相同的功能:
const myTemplate = (page: number, id: number) => `page=${page}&id=${id}`;
const formattedWithTemplate = myTemplate(123, 456);
console.log(formattedWithTemplate); // page=123&id=456
如果您打算修改
String
的原型,并且之前的警告没有阻止您,那么您可以使用or方法允许TypeScript识别您希望String
值具有format()
方法:
/*谢谢!你提出的第一个解决方案很有魅力!