react-i18next: i18n object is not typed at all in V12
After updating to v12 and following the instructions here, I get the following TS errors:
const {i18n} = useTranslation();
i18n.language
i18n.changeLanguage
^^^ TS2339: Property 'language' does not exist on type 'i18n'.
error TS2339: Property 'changeLanguage' does not exist on type 'i18n'.
and in the initialize function, as well
i18n.use(initReactI18next).init({
// ...
});
TS2339: Property 'use' does not exist on type 'typeof import("/Users/me/project/src/i18next")'.
But those properties are clearly there when you console.log(i18n)
, along with lots of others. It appears that the i18n object returned by useTranslation has not been typed at all.
How do I set up the i18next.d.ts file so that the i18n
object returned by useTranslation()
is properly typed?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (5 by maintainers)
I think I found the cause while doing a minimal reproduction and copying over more and more of my real config to it.
I think the problem is the
i18next.d.ts
file in the root of my project, that version 22 makes me add. When that file is added, then allimport x from "i18next"
lines imports this file instead of the node module.Since that module doesn’t actually declare anything inside of the
i18next
module, it will not find any functions in the other files.I moved it to a
@types/
directory, and not it seems to be working again.I still don’t know why
import _ from "i18next"
would pick up this file in my repo but not in my minimal reproduction.I have my d.ts file in
@types/
but I still have the same issue. Did anyone solve it?“typescript”: “4.9.5” “i18next”: “22.4.12” “react-i18next”: “12.2.0”
UPD: Works only if I put it into
global.d.ts
instead, maybe it will help someone as a workaroundHello guys, I have the same issue: Versions I use:
"i18next": "22.0.5"
"react-i18next": "12.0.0"
"typescript": "3.9.10"
Is there any solution?
Hey @vashchukmaksim, just add
@types
to yourtsconfig.json
file underinclude
:I fixed it with typescript upgrade. I have 4.1.6 now and everything works fine 😃
@pedrodurek I have this problem too.
i18n.ts file
i18next.d.ts file
Both files are under
src
directory in appAnd it complains about this
detailed info:
TS2339: Property 'use' does not exist on type 'typeof import("/Users/tomaskretek/inQool/som.inqool.cz-client/node_modules/.pnpm/i18next@22.0.5/node_modules/i18next/index")'.
TS2339: Property 'isInitialized' does not exist on type 'typeof import("/Users/tomaskretek/inQool/som.inqool.cz-client/node_modules/.pnpm/i18next@22.0.5/node_modules/i18next/index")'.
TS2339: Property 'init' does not exist on type 'typeof import("/Users/tomaskretek/inQool/som.inqool.cz-client/node_modules/.pnpm/i18next@22.0.5/node_modules/i18next/index")'.