Getting TypeScript/IntelliSense autocomplete to work with next-intl
's <NextIntlClientProvider />
#803
codesfromshad
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
According to
next-intl
's docs,next-intl
recommends that you uselodash
'spick()
function to pass only the messages required by the<NextIntlClientProvider />
instead of the entire message object:However, the above approach doesn't work with TypeScript and Intellisense autocomplete. To have it working, I came up with the following solution.
Step 1: Configure Global Types
First, define your message types globally as recommended by the
next-intl
documentation. This ensures type safety for your message keys. Add the following to yourglobal.d.ts
file:Also, include
global.d.ts
in yourtsconfig.json
to ensure TypeScript recognizes these global types:Step 2: Define
MessageKeys
TypeCreate a
MessageKeys.ts
file in your types directory (e.g.,/src/types
). This file will define theMessageKeys
type, which helps in leveraging TypeScript's type checking:Step 3: Use
<NextIntlClientProvider />
with TypeScriptWhen using the
<NextIntlClientProvider />
, ensure that the messages are correctly typed. This facilitates autocomplete and type checking:Directory structure:
End result:
Beta Was this translation helpful? Give feedback.
All reactions