Why String.Format does not work in TypeScript?

String.Format does not work in TypeScript.

Error:

The property ‘format’ does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'

In the following code:

attributes["Title"] = String.format(
    Settings.labelKeyValuePhraseCollection["[WAIT DAYS]"],
    originalAttributes.Days
);

What is the correct way to format strings in TypeScript if String.Format is not available?

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.

I solved it using the following approach:

I created a function:

export function FormatString(str: string, ...val: string[]): string {
  for (let index = 0; index < val.length; index++) {
    str = str.replace(`{${index}}`, val[index]);
  }
  return str;
}

I used it like this:

FormatString("{0} is {1} {2}", "This", "formatting", "hack");