As of TypeScript 1.4, string interpolation is supported. Here’s how you can use it:
let a = "Hello";
let b = "World";
let text = `${a} ${b}`;
This code will compile to:
var a = "Hello";
var b = "World";
var text = a + " " + b;
The JavaScript String object does not have a format function, and TypeScript does not add new methods to native objects. To achieve a similar result, you need to extend the String interface and provide an implementation:
interface String {
format(...replacements: string[]): string;
}
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
;
});
};
}
You can then use this feature like so:
let myStr = 'This is an {0} for {0} purposes: {1}';
alert(myStr.format('example', 'end'));
String interpolation (via template literals) is an ECMAScript 6 feature and is often more intuitive. However, to use it for a String.format use case, you might need to wrap it in a function to handle dynamic formatting. String interpolation is usually used inline with variables, and using it to replace format strings requires mapping arguments manually.
For example:
// Works
let myFormatString = 'This is an {0} for {0} purposes: {1}';
// Compiler warnings (a and b not yet defined)
let myTemplateString = `This is an ${a} for ${a} purposes: ${b}`;
To use string interpolation effectively, define a function:
function myTemplate(a: string, b: string) {
let myTemplateString = `This is an ${a} for ${a} purposes: ${b}`;
return myTemplateString;
}
alert(myTemplate('example', 'end'));
For format strings used as resources, there isn’t a straightforward way to load a template string from a data source without using eval.