TypeScript integration
next-intl
integrates with TypeScript out-of-the box without additional setup. You can however provide the shape of your messages to get autocompletion and type safety for your namespaces and message keys.
messages.json
{
"About": {
"title": "Hello"
}
}
About.tsx
function About() {
// ✅ Valid namespace
const t = useTranslations('About');
// ✖️ Unknown message key
t('description');
// ✅ Valid message key
return <p>{t('title')}</p>;
}
To do this, add a global type definition file in your project root (e.g. global.d.ts
):
global.d.ts
// Use type safe message keys with `next-intl`
type Messages = typeof import('./messages/en.json');
declare interface IntlMessages extends Messages {}
You can freely define the interface, but if you have your messages available locally, it can be helpful to automatically create the interface based on a messages sample by importing it.
⚠️
If you're encountering problems, please double check that:
- Your interface is called
IntlMessages
. - You're using TypeScript version 4 or later.
- The path of your
import
is correct. - Your type declaration file is included in
tsconfig.json
. Your editor has loaded the most recent type declarations. When in doubt, please restart.