Next-i18next: typescript I18nContext

Created on 4 Aug 2020  Â·  7Comments  Â·  Source: isaachinman/next-i18next

Describe the bug

typescript I18nContext type is not working correctly I got this error

import { I18nContext } from "next-i18next";

export const useActiveLang = () => {
const {
i18n: { language: activeLang },
} = useContext(I18nContext);
return { activeLang };
};

image

Occurs in next-i18next version

"next-i18next": "^6.0.1",

https://github.com/isaachinman/next-i18next#accessing-the-current-language

Here is provided code from the documentation I guess it has to be working without any issue but same error

image

image

help wanted

Most helpful comment

Can we get a bump to i18n-react 11.7.1 so we can have this fix?

Edit. Oh we got it already, just needs merging and a bump in our version.

All 7 comments

Interesting. It definitely is exported, but it seems like there's an issue with TS/module resolution here.

If you do a import * as nextI18NextExports from 'next-i18next', you'll see that I18nContext is indeed there. I had a quick look at this, but am not sure what the exact problem is – TS is unable to see the named export for some reason.

Would be very happy to have some community support on this one.

As I see in types.d.ts file I18nContext is exported as a type but for this specific scenario we need as value to pass useContext as an argument

image

@tokomeno Yes, it's exported as a type because _it is_ a type. If you remove that line in types.d.ts, TypeScript will simply complain that next-i18next has no exported member named I18nContext, even though it does – TS is simply failing to pick up the export. As far as I can tell it doesn't actually have anything to do with the types.

Can we get a bump to i18n-react 11.7.1 so we can have this fix?

Edit. Oh we got it already, just needs merging and a bump in our version.

@haggen #799 introduces a bunch of other changes which shouldn't be necessary. If you open a PR just to bump react-i18next, I'll merge it immediately.

Done @isaachinman

finally got some time for this

@isaachinman it is exported in the implementation, but in the typedef, it is reexported as type only, that's why the tsc error: 'I18nContext refers to a type, but used as a variable'

I could create an minimally viable PR later

Was this page helpful?
0 / 5 - 0 ratings