Search code examples
javascriptstringtypescriptstring-formatting

String.Format 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; }'.

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

Solution

  • You can declare it yourself quite easily:

    interface StringConstructor {
        format: (formatString: string, ...replacement: any[]) => string;
    }
    
    String.format('','');
    

    This is assuming that String.format is defined elsewhere. e.g. in Microsoft Ajax Toolkit : http://www.asp.net/ajaxlibrary/Reference.String-format-Function.ashx