-
-
Notifications
You must be signed in to change notification settings - Fork 627
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to type translation keys #1523
Conversation
4a4bf70
to
c010df2
Compare
index.d.ts
Outdated
/** | ||
* Gets the defined translation keys if they have been overridden, 'string' type otherwise | ||
*/ | ||
type TranslationKeys = I18nTranslationKeys['keys'] extends string ? I18nTranslationKeys['keys'] : string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This raises a typescript error, so it should be done another way, but I have no idea how... I thought of adding { keys: string }
in I18nTranslationKeys
, but in that case we only will benefit the typescript autocompletion and no errors in case of incorrect keys. The most important is really the checks, not the completion.
It's best to solve the problem of this PR with a type augmentation so that the keys may be added 100% using typescript definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(btw, npm run tsc
should be run in the CI as well, since current CI checks are green but they shouldn't)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a long thought about this, and it might have no solution as it is. I can't think of any way to augment a type strictly, and make it prevail over the default type (here, augment the keys with 'a' | 'b'
and cancel out the default string
type).
It works here as it is, but it needs a @ts-ignore
, and I doubt that it's acceptable.
Maybe augmenting the type is a bad approach? In my opinion that's the most convenient solution as a consumer of the package, but I'm not sure we can implement it.
This is a first step towards solving i18next#1504 It allows you to type your translation keys freely. Which allows you to type them using the template literals of Typescript 4.1 for nested keys.
I'm not 100% sure why users insist on having the keys typed -> personally I add keys on the fly and let them added to the JSON by the saveMissing feature of i18next -> typed you have to manually add keys upfront to not break (or do I misunderstand how this works?) |
//cc: @pedrodurek |
Indeed, such a feature makes it fine to work with the keys (I didn't know it to be honest, thanks!). But typing provides the following advantages:
Also, it feels more natural to add my keys in the JSON then use them with the autocompletion 馃槉 |
Oh... I just noticed that this work has already been done on But I think it should be in here as well. It should be available to all frameworks! |
Hey @pierpo thanks flagging that, as soon as we fixed all issues pointed out for the new types (almost there), we can certainly bring the types to here, if it's interesting for everyone. |
I like the comment here: #1504 (comment) would be great to keep things opt-in and not again break existing builds (the start on react-i18next with the new types was rather though). |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This is a first step towards solving #1504
It allows you to type your translation keys freely. Which allows you to type them using the template literals of Typescript 4.1 for nested keys 馃槈 (see #1504).
Usage
In my project, I augment the module definition in a type definition file
typings/i18next.d.ts
.Then in the whole project, I may use the
t
function like this:Same if I use
react-i18next
.Advanced usage
Deeply typed keys using Typescript 4.1
What's left to do
Well, it's just a first step. I'm open to suggestions: overriding the interface this way feels hacky, but I found no other way.
It also needs documentation somewhere.
only relevant code is changed (make a diff before you submit the PR)
run tests
npm run test
tests are included
documentation is changed or added