I am attempting to customize the TFunction from the i18next package.
The goal is to enforce typing on the i18n keys being used as follows:
t('invalid-key') // should be invalid
t('key1') // should be valid
To achieve this, I created an i18next.d.ts
file to override the TFunction type:
type Keys = 'key1' | 'key2'
declare module 'i18next' {
export type TFunction = (key: Keys) => string
}
While this solution works, whenever I try to import this type from another file, the TFunction reverts back to its original type.
Any suggestions on how to properly import the keys in the declaration file?
Coincidentally, importing works when done through the useTranslation hook of the react-i18next package:
import { I18nKey } from '../modules/core/i18n/i18nKey'
declare module 'react-i18next' {
export function useTranslation(): {
t: (key: I18nKey) => string
}
}