无法使用打字稿进行模块扩充



我只是按照说明向react-i18next实例添加了正确的类型,如下所示:https://react.i18next.com/latest/typescript#create-a声明-文件

然而,在创建了提到的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

由于某种原因,类型定义文件和使用它的实际源代码似乎不能位于同一文件夹中。我刚刚将类型定义文件移到types文件夹中,后者包含在tsconfig.json中,现在它工作得很好:

// tsconfig file
"include": ["./src/**/*", "./types"],

root/
├─ src/
├─ types/
│  ├─ react-i18next.d.ts
├─ tsconfig.json

最新更新