-
-
Notifications
You must be signed in to change notification settings - Fork 257
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
Type safe message access #90
Comments
Thank you for opening this issue, I'd be interested in this too as it will enhance the DX quite a bit! I'm not sure yet what's the best way to tackle this. I can think of the following approaches:
useTranslations<Messages>(…)
const {useTranslations, …} = createTypedIntlModules<Messages>(…);
Not sure if there are other options, but from these I'd largely prefer the third one the user doesn't have to change any call site of the provided modules. Another part is typing the The other aspect is how to create the type. I'd guess there are some utilities from TypeScript to generate this, I'd image something like If you have other ideas or are interested in helping out to implement this please let me know! |
Hey @coffeedoughnuts, I found some time to look into this topic and I think I've found a solution that works! Would you like to try out a prerelease version of this feature in your app? I'd be really happy about some feedback if you have the time. Here's how to try the prerelease: npm install use-intl@2.4.1-alpha.5 next-intl@2.4.1-alpha.5 … and here are the docs for the new feature: https://next-intl-git-feat-90-type-safe-messages-amann.vercel.app/docs/usage/typescript. /cc @ishigami since you accidentally found an earlier prerelease of this feature in #94 😁. Also /cc @andrevenancio since you already provided helpful feedback in the past! Maybe you're curious 🙂 |
Hey - I'm so sorry, I had some time off from work and completely forgot to reply to you after I returned! I shall try your solution later today and report back; thank you for looking into it! |
small point, in case anyone else is trying this alpha, I could only get it working if I specifically installed as for the implementation, it's great! as this package is quite closely related to
in the linked documentation, becomes:
it's a pretty minor change but it follows's next's convention for solving a similar situation |
Thank you for trying out the pre-release and your feedback! 🙌 After another thought I've renamed Hmm, about the name of the suggested file: I think Next.js has to use their own file, since they automatically write to this file and therefore they need a separate one. As for the the file that is currently suggested in the docs, this is just a snippet that can live alongside other library definitions. At least personally I wouldn't want to have a separate file per library that I'm declaring types for, especially if they only contain a few lines each. But I'm not really sure what the best practice is here for TypeScript projects. Here are some links I found:
There's no clear suggestion here though as far as I can tell. In projects I've worked on so far I've seen I tried to not really take a stance here with this phrasing:
I wouldn't want the user to have to create a separate library definition for this library, therefore I think we should stick to a less commanding suggestion. I'd be really curious though if there is an established best practice for these types of files. |
Describe the solution you'd like
I would find it very helpful if message access was type safe, for example:
given messages like this:
it would be good to have typescript fail on these two examples:
Additional context
I appreciate this could be hard for splitting message sets across different pages (as you may not know which message set is available at any given time) so having all return types be possibly undefined could be a compromise. The aim of the feature is not so much to guarantee access safety but to facilitate auto completion of keys to avoid typos etc.
The text was updated successfully, but these errors were encountered: