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

如何修复TS2339错误:typeof Intl不存在DisplayNames属性?

修复TS2339错误:typeof Intl不存在'DisplayNames'属性

方案1:更新tsconfig.json的lib配置

Intl.DisplayNames属于ES2020及以上的国际化API,TypeScript默认类型库可能未包含它的定义。修改tsconfig.json中的lib数组,添加对应Intl类型库:

{
  "compilerOptions": {
    "lib": ["ES2020", "ES2020.Intl", "DOM"] // 可根据项目需求调整其他lib项
  }
}

若需要更全面的最新Intl API支持,可替换为"ESNext.Intl"。

方案2:升级TypeScript版本

TypeScript从4.0版本开始正式支持Intl.DisplayNames的类型定义。如果你的TS版本低于4.0,执行以下命令升级:

npm install typescript@latest --save-dev
# 或使用yarn
yarn add typescript@latest --dev

方案3:类型断言(临时绕过,不推荐长期使用)

若暂时无法升级或修改配置,可通过类型断言跳过TS类型检查:

export const getLanguageName = (locale: string | null) => {
  const localeName = new (Intl as any).DisplayNames([locale], { type: 'language' });
  return localeName.of(locale);
};

注意:该方法会丢失类型校验优势,仅作为临时应急方案。

补充:处理null边界情况

你的代码中locale可能为null,建议添加边界判断避免运行时错误:

export const getLanguageName = (locale: string | null) => {
  if (!locale) return '';
  const localeName = new Intl.DisplayNames([locale], { type: 'language' });
  return localeName.of(locale);
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 16:01:13