Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript 4.1 setup issue #1239

Closed
KestasVenslauskas opened this issue Jan 19, 2021 · 2 comments
Closed

Typescript 4.1 setup issue #1239

KestasVenslauskas opened this issue Jan 19, 2021 · 2 comments

Comments

@KestasVenslauskas
Copy link

KestasVenslauskas commented Jan 19, 2021

Here you have an example which works fine:
https://github.com/i18next/react-i18next/blob/master/example/react-typescript4.1/no-namespaces/src/App.tsx

I have working setup like that. But the problem is when I try to introduce template literals.
Let's say I have type:
type MyType = 'part1' | 'part2' | 'part3';

And I want to get the translation like so:

const part: Part = 'part1'; const {t} = useTranslation('description.${part}'); //Error
This will give me TS error.

Only if single raw string is used TS will understand that this is ok.

@pedrodurek
Copy link
Member

pedrodurek commented Jan 19, 2021

Hey @KestasVenslauskas, this a typescript limitation which is going to be handled on typescript 4.2.
In the meantime, you can solve this problem by asserting your template string as const. Like this:

const {t} = useTranslation(`description.${part}` as const);

Take a look at this comment for more info: #1193 (comment)

@KestasVenslauskas
Copy link
Author

Thaks! That worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants