【问题标题】:i18next: How to avoid TS6133-error 'foo' is declared but its value is never read?i18next:如何避免 TS6133-error 'foo' 被声明但它的值从未被读取?
【发布时间】:2020-10-02 21:32:41
【问题描述】:

[已解决] 我想知道如何避免与 i18next 相关的 TS6133-error: 'var' is declared but its value is never read?基本上我有一个函数labelForTeaserStyle(style: TeaserStyle),在这行代码中读取:

<Typography variant="subtitle1" color="gray">
    Style: {labelForTeaserStyle(style)}
</Typography>

function labelForTeaserStyle(style: TeaserStyle) {
  switch (style) {
    case TeaserStyle.Default:
      return 'Default'

    case TeaserStyle.Light:
      return 'Light'

    case TeaserStyle.Text:
      return 'Text'
  }

但是,当将 i18next 应用到这行代码时,它会抛出 TS6133-error 'labelForTeaserStyle' is declared but its value is never read

我已经阅读了关于在导致错误的行之前使用 /* tslint:disable:no-unused-variable */ 的信息,但这无济于事。我读过的另一个建议是在 tsconfig.json 中停用 noUnusedLocals,但这对我来说不是一个选项,因为我想保持此选​​项处于激活状态。

现在对我来说的问题是,我能否以某种方式嵌入 i18next 代码,以便编译器仍将 labelForTeaserStyle() 识别为正在使用/读取?

使用 i18next 编写代码

<Typography variant="subtitle1" color="gray">
    {t('styleInterpolated', {labelForTeaserStyle(style)})}
</Typography>

按键

"styleInterpolated": "Style: {{function}}"

【问题讨论】:

  • 请将解决方案作为答案发布,以便其他有相同问题的人可以找到。
  • 对不起,我是新手。很快就会发布它作为答案。

标签: javascript i18next react-i18next


【解决方案1】:

解决方案

我找到了我的问题的答案。问题是我同时实现了 ESLint 和 TSLint,因此单独使用 // @ts-ignore 并不能解决问题。解决方案是同时添加/* eslint-disable */。这是工作代码:

/* eslint-disable */
// @ts-ignore
function labelForTeaserStyle(style: TeaserStyle) {
  (...)
}

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 2019-06-23
    • 2023-03-15
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2018-09-28
    • 2021-02-25
    相关资源
    最近更新 更多