amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.58k stars 236 forks source link

Type `useMessages()` hook using `IntlMessages` #1452

Open astralarya opened 3 weeks ago

astralarya commented 3 weeks ago

Is your feature request related to a problem? Please describe.

useMessages() hook does not get proper typing after declaring IntlMessages.

For example:

declare global {
  // Use type safe message keys with `next-intl`
  type IntlMessages = {
    foo: {
      bar: string,
      baz: string,
    }
  }
}

function MyComponent() {
  const messages = useMessages();  // expect this to have type IntlMessages
}

Describe the solution you'd like

I would like the useMessages() hook to respect the type provided by IntlMessages

Describe alternatives you've considered

I've considered casting in place every time I invoke the hook, but this is error prone.

amannn commented 2 weeks ago

Sounds good, thanks for the report! This will likely make it into the upcoming v4 release: https://github.com/amannn/next-intl/pull/1489