i18next/i18next

Not existing context not detected as type error if covered by string union

Offen

#2.172 geöffnet am 12.04.2024

 (13 Kommentare) (2 Reaktionen) (0 zugewiesene Personen)JavaScript (637 Forks)batch import
help wantedtypescript

Repository-Metriken

Stars
 (7.194 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 2h 18m) (3 gemergte PRs in 30 T)

Beschreibung

🐛 Bug Report

I was told to move this issue here from the https://github.com/i18next/react-i18next/issues/1743 repo.

When you use a string union as the context parameter it's possible to supply values for which a context does not exist and it does not result in an error. If you just use the plain string value or a const it works.

To Reproduce

A minimal reproducible example.

{
  "translation": {
    "testContext1_Test1": "EN: Context1 Test1",
    "testContext1_Test2": "EN: Context1 Test2",
  },
}
const App = () => {
  const { t } = useTranslation('translation');

  return (
    <div>
      <p>
        {t('testContext1', { context: 'Test1' as 'Test1' | 'Test2' | 'Test3' })}
      </p>
      <p>{t('testContext1', { context: 'Test2' })}</p>
      <p>{t('testContext1', { context: 'Test2' as const })}</p>
      <p>
        {t('testContext1', { context: 'Test3' as 'Test1' | 'Test2' | 'Test3' })}
      </p>
      {/* Error: Type '{ context: "Test3"; }' is not assignable to type 'string' */}
      <p>{t('testContext1', { context: 'Test3' })}</p>
      {/* Error: Type '{ context: "Test3"; }' is not assignable to type 'string' */}
      <p>{t('testContext1', { context: 'Test3' as const })}</p>
    </div>
  );
};

Expected behavior

String unions should be correctly detected as errors if they have cases which are not covered by the context.

Your Environment

  • i18next version: 23.11.1

Contributor Guide