#reactjs #typescript #react-i18next
#reactjs #typescript #реагировать-i18next
Вопрос:
Я просто следовал инструкциям по добавлению правильных типов в мой экземпляр react-i18next, как описано здесь: https://react.i18next.com/latest/typescript#create-a-declaration-file
Однако после создания упомянутого react-i18next.d.ts
файла я начинаю получать ошибки о неэкспортированных членах модуля react-i18next
, например, useTranslation hook:
Module '"react-i18next"' has no exported member 'useTranslation'.ts(2305)
Вот содержимое моего react-i18next.d.ts
файла
// import the original type declarations
import 'react-i18next';
// import all namespaces (for the default language, only)
import translation from 'locales/en/translation.json';
import toast from 'locales/en/toasts.json';
// react-i18next versions lower than 11.11.0
declare module 'react-i18next' {
// and extend them!
interface Resources {
translation: typeof translation;
toast: typeof toast;
}
}
declare module 'react-i18next' {
// and extend them!
interface CustomTypeOptions {
// custom namespace type if you changed it
defaultNS: 'translation';
// custom resources type
resources: {
translation: typeof translation;
toast: typeof toast;
};
}
}
Я использую typescript@4.4.4
Комментарии:
1. это потому, что базовый модуль не был загружен, только ваш
2. Я подозреваю, что это какое-то затенение модуля
Ответ №1:
По какой-то причине кажется, что файл определения типа и фактический исходный код, который его использует, не могут находиться в одной папке. Я просто переместил файл определения типов в types
папку, которая включена в последнюю tsconfig.json
версию, и теперь она работает отлично:
// tsconfig file
"include": ["./src/**/*", "./types"],
root/
├─ src/
├─ types/
│ ├─ react-i18next.d.ts
├─ tsconfig.json