You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Intl对象添加更新类型以解决supportedValuesOf的TS类型错误?

解决Intl.supportedValuesOf的TypeScript类型错误

方法一:升级TypeScript版本

从TypeScript 4.9版本开始,官方类型定义已经内置了Intl.supportedValuesOf的支持。如果你的项目TS版本低于4.9,直接升级到最新稳定版即可解决这个类型错误。

方法二:手动扩展全局类型(无需升级TS)

如果暂时无法升级TS版本,可以通过自定义类型声明文件来扩展Intl的类型:

  • 在项目的src目录下创建一个global.d.ts文件(文件名可自定义,只要是.d.ts后缀即可)
  • 在文件中添加以下代码:
declare global {
  namespace Intl {
    /**
     * 返回指定类别的所有支持值
     * @param key 要查询的类别,比如"timeZone"、"currency"、"calendar"等
     */
    function supportedValuesOf(key: "timeZone" | "currency" | "calendar" | "collation" | "numberingSystem" | "dateTimeFormat" | "numberFormat"): string[];
  }
}

export {};
  • 确保tsconfig.json的include数组包含这个声明文件,示例配置:
{
  "include": ["src/**/*", "src/global.d.ts"]
}

完成以上操作后,TypeScript就能识别Intl.supportedValuesOf方法,无需使用@ts-ignore。

内容的提问来源于stack exchange,提问作者Ar26

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 07:54:55