Skip to content

Take fallbackNs into consideration for TFunction key type check #1268

Closed
@Haaxor1689

Description

@Haaxor1689

🐛 Bug Report

When using the method of redeclaring Resources to provide type check for TFunction the generated keys also contain namespace that is not a fallback one which results in typescript being ok with a key that doesn't resolve into a translation.

To Reproduce

const resources = {
	sk: {
		foo: {
			a: 'A',
		},
		goo: {
			b: 'B',
		},
	},
};

i18next.use(initReactI18next).init({
	resources,
	ns: Object.keys(resources.sk),
	fallbackNS: 'foo',
        // ...
});

declare module 'react-i18next' {
	type DefaultResources = typeof resources['sk'];
	// eslint-disable-next-line
	interface Resources extends DefaultResources {}
}

With this setup none of these usages show a type error:

const { t } = useTranslation();
t('a') // -> "A", OK
t('b') // -> "b", Wrong, prints the key
t('foo:a') // -> "A", OK
t('goo:b') // -> "B", OK

Another issue with key type check arises when you specify a namespace in useTranslate hook.

const { t } = useTranslation('goo');
t('a') // -> "A", Wrong, shows a type error
t('b') // -> "B", OK
t('foo:a') // -> "A", Wrong, shows a type error
t('goo:b') // -> "B", Wrong, shows a type error

Expected behavior

Add option to also specify which of the namespaces are also fallback namespaces so keys of non fallback namespaces don't pass through type check in TFunction.

const { t } = useTranslation();
t('b') // -> Should show type error
const { t } = useTranslation('goo');
t('a') // -> Shouldn't show a type error
t('foo:a') // -> Shouldn't show a type error
t('goo:b') // -> Shouldn't show a type error

Your Environment

  • i18next version: 19.9.1
  • react-i18next version: 11.8.8
  • typescript version: 4.1.2

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions