Not existing context not detected as type error if covered by string union
Author: stefan-schweigerCreated Apr 12, 2024Updated Mar 27, 2026
Labelshelp wantedtypescript
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
Source: i18next/i18next