相关疑难解决方法(0)

如何使用 TypeScript 键入检查 i18n 词典?

是否可以在react-i18next字典中键入检查现有键?因此,如果密钥不存在,TS 会在编译时警告您。

例子。

假设,我们有这本字典:

{
  "footer": {
    "copyright": "Some copyrights"
  },

  "header": {
    "logo": "Logo",
    "link": "Link",
  },
}
Run Code Online (Sandbox Code Playgroud)

如果我提供不存在的密钥,TS 应该会爆炸:

const { t } = useTranslation();

<span> { t('footer.copyright') } </span> // this is OK, because footer.copyright exists
<span> { t('footer.logo') } </span> // TS BOOM!! there is no footer.logo in dictionary
Run Code Online (Sandbox Code Playgroud)

这种技术的正确名称是什么?我很确定我不是唯一一个要求这种行为的人。

它是react-i18next开箱即用的吗?是否有 APIreact-i18next以某种方式扩展库以启用它?我想避免创建包装函数。

typescript i18next react-i18next

10
推荐指数
2
解决办法
1万
查看次数

标签 统计

i18next ×1

react-i18next ×1

typescript ×1