nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.65k stars 465 forks source link

TypeScript Issue with nuxtApp.$i18n in defineNuxtPlugin #2955

Closed daniil4udo closed 1 week ago

daniil4udo commented 1 month ago

Environment


Reproduction

When using nuxtApp that is passed to defineNuxtPlugin, there is a TypeScript issue indicating that nuxtApp.$i18n is of type unknown. This problem arises when trying to set the locale cookie using the $i18n property.

export default defineNuxtPlugin(nuxtApp => {
    const route = useRoute();

    if (route.query.lang)
        nuxtApp.$i18n.setLocaleCookie(route.query.lang);
});

Describe the bug

Expected Behavior nuxtApp.$i18n should be recognized by TypeScript as the proper type, allowing the setLocaleCookie method to be called without type errors.

Actual Behavior TypeScript reports an error: 'nuxtApp.$i18n' is of type 'unknown'.

Steps to Reproduce Add '@nuxtjs/i18n' to the modules Define a Nuxt plugin using defineNuxtPlugin. Attempt to use nuxtApp.$i18n within the plugin. Observe the TypeScript error indicating that nuxtApp.$i18n is of type unknown.

Additional context

After the inspection .nuxt I saw there is a correct types added, and

declare module '#app' {
  interface NuxtApp {
    $i18n: ExportedGlobalComposer & Composer & NuxtI18nRoutingCustomProperties<LocaleObject[]>
  }
}

although it still dosnt work

Logs

No response

github-actions[bot] commented 1 month ago

Would you be able to provide a reproduction? πŸ™

More info ### Why do I need to provide a reproduction? Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making. ### What will happen? If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect. If `needs reproduction` labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it. ### How can I create a reproduction? We have a couple of templates for starting with a minimal reproduction: πŸ‘‰ [Reproduction starter (v8 and higher)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/v8) πŸ‘‰ [Reproduction starter (edge)](https://stackblitz.com/fork/github/BobbieGoede/nuxt-i18n-starter/tree/edge) A public GitHub repository is also perfect. πŸ‘Œ Please ensure that the reproduction is as **minimal** as possible. See more details [in our guide](https://nuxt.com/docs/community/reporting-bugs/#create-a-minimal-reproduction). You might also find these other articles interesting and/or helpful: - [The Importance of Reproductions](https://antfu.me/posts/why-reproductions-are-required) - [How to Generate a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve)
kazupon commented 1 week ago

Closing due to inactivity. Please open a new issue with a reference to this one if you can follow up with more information.

thanks!