如何添加类型以在节点 v12 中使用 Intl.ListFormat

Jam*_*son 7 javascript node.js typescript

我正在使用nodeJS v12.10.0 这支持Intl.ListFormat 使用Typescript v3.6.3

但是,当使用打字稿进行编译时,出现Property 'ListFormat' does not exit on type 'typeof Intl' (ts2339)错误。

我尝试过谷歌搜索并找到了这个和其他建议,但没有任何效果。

我发现的共识似乎是使用文件name.d.ts并以某种方式扩展Intl那里的对象,但我无法做到。

我仍在学习 typescript,但熟悉 javascript。

小智 5

我发现接受的答案对我不起作用。我使用了以下内容:

declare namespace Intl {
  class ListFormat {
    constructor(locales?: string | string[], options?: Intl.ListFormatOptions);
    public format: (items: string[]) => string;
  }
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*son 2

在文件中添加以下内容something.d.ts似乎可行。

declare namespace Intl {
  class ListFormat {
    public format: (items: [string?]) => string;
  }
}
Run Code Online (Sandbox Code Playgroud)

我从最初的谷歌搜索中得到了这个想法,但后来阅读这篇文章帮助我解决了这个问题。

发布此内容以防对其他人有帮助。