Intl.supportedValuesOf 类型“typeof Intl”上不存在属性“supportedValuesOf”

问题描述 投票:0回答:2

我使用 dayJS 作为我的日期时间库,并希望呈现所有时区的列表。
我尝试使用 Intl 对象:

Intl.supportedValuesOf("timeZone")

但我收到打字稿错误:
TS2339: Property 'supportedValuesOf' does not exist on type 'typeof Intl'.

我可以获得值,但我想避免使用 @ts-ignore 符号

如何为 Intl 对象添加更新的类型?

typescript
2个回答
4
投票

参见https://github.com/microsoft/TypeScript/issues/49231

“更改在 Typescript 5.1 之前不会发布”


0
投票

我也刚刚遇到这个问题。我将详细说明已经提供的答案。

如果你想在 Typescript 5.1 之前的 Typescript 版本上访问方法

Intl.supportedValuesOf()
,你可以创建一个文件
Intl.d.ts
(我认为这个名称实际上是任意的)并将以下内容复制到文件中:

declare namespace Intl {
  type Key = "calendar" | "collation" | "currency" | "numberingSystem" | "timeZone" | "unit";

  function supportedValuesOf(input: Key): string[];
}

然后,在 tsconfig.json 文件中,在“include”属性中添加我们刚刚创建的

Intl.d.ts
文件的相对路径,如下所示:

{
  "compilerOptions": {
    ...
  },
  "include": ["/types/Intl.d.ts"]
}

您可能需要重新启动应用程序才能看到更改

© www.soinside.com 2019 - 2024. All rights reserved.