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 error: Context is not assignable to type 'string' #1742

Closed
VasilNikolov opened this issue Apr 11, 2024 · 5 comments
Closed

Typescript error: Context is not assignable to type 'string' #1742

VasilNikolov opened this issue Apr 11, 2024 · 5 comments
Assignees

Comments

@VasilNikolov
Copy link

🐛 Bug Report

The issue is that we are having the following error when using context in a typescript project.

Type '{ context: "Example"; }' is not assignable to type 'string'.

image

To Reproduce

A minimal reproducible example.

// Paste your code here

Expected behavior

A clear and concise description of what you expected to happen.

// Paste the expected results here

Your Environment

  • runtime version: i.e. node v14, deno, browser xy
  • i18next version: i.e. 19.5.3
  • os: Mac, Windows, Linux
  • any other relevant information
@stefan-schweiger
Copy link

stefan-schweiger commented Apr 11, 2024

@VasilNikolov I had a similar issue some time ago and one of the answers was that context is really only meant for known values and that there are other fallback mechanisms. I don't think even today that I fully agree with that but I have changed my code accordingly..

@VasilNikolov
Copy link
Author

@VasilNikolov I had a similar issue some time ago and one of the answers was that context is really only meant for known values and that there are other fallback mechanisms. I don't think even today that I fully agree with that but I have changed my code accordingly..

And what are considered known values?

@stefan-schweiger
Copy link

Everything specified in your translation file as possible context values.

@marcalexiei
Copy link
Member

Like @stefan-schweiger pointed out, you have to specify all possible values for the context value:

asdf is not present in your translation keys.
If you have asdf as possible context value it should be specified in translation resources:

//...
testContext1: 'EN: {{context}}',
testContext1_Test1: 'EN: Context1 Test1',
testContext1_Test2: 'EN: Context1 Test2',
testContext1_asdf: 'EN: Context1 asdf', // ⬅️⬅️⬅️⬅️
// ...

@VasilNikolov
Copy link
Author

Like @stefan-schweiger pointed out, you have to specify all possible values for the context value:

asdf is not present in your translation keys. If you have asdf as possible context value it should be specified in translation resources:

//...
testContext1: 'EN: {{context}}',
testContext1_Test1: 'EN: Context1 Test1',
testContext1_Test2: 'EN: Context1 Test2',
testContext1_asdf: 'EN: Context1 asdf', // ⬅️⬅️⬅️⬅️
// ...

Yes, but when you introduce a json store for the translations it again throws the errors.
Example

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

4 participants