String.Format 在 TypeScript 中不起作用

新手上路,请多包涵

String.FormatTypeScript 中不起作用。

错误:

>  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 );

”`

原文由 Антон Степанов 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 895
2 个回答

你可以很容易地自己 声明 它:

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

String.format('','');

这是假设 String.format 在其他地方 定义 的。例如在 Microsoft Ajax 工具包中: http ://www.asp.net/ajaxlibrary/Reference.String-format-Function.ashx

原文由 basarat 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您使用的是 NodeJS,则可以使用内置的 util 函数:

 import * as util from "util";
util.format('My string: %s', 'foo');

文档可以在这里找到: https ://nodejs.org/api/util.html#util_util_format_format_args

原文由 SLdragon 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题