Next.js Discord

Discord Forum

Next-intl type error accessing field with dynamic string

Unanswered
Rex posted this in #help-forum
Open in Discord
RexOP
en.json
"Languages": {
    "en": "English",
    "it": "Italian",
    "es": "Spanish",
    "fr": "French",
    "de": "German",
    "ru": "Russian",
    "dk": "Danish",
    "se": "Swedish",
    "in": "Hindi",
    "cn": "Chinese",
    "jp": "Japanese",
    "another_language": "Another language"
  },
...

page.tsx
import { useTranslations } from 'next-intl';
const l = useTranslations('Languages');
...
l(word.language) -> here I got the error


Error:
Type error: Argument of type 'string' is not assignable to parameter of type 'MessageKeys<{ en: string; it: string; es: string; fr: string; de: string; ru: string; dk: string; se: string; in: string; cn: string; jp: string; another_language: string; }, "it" | "en" | "es" | "fr" | "de" | "ru" | ... 5 more ... | "another_language">'.
  143 |                 <p className="object-field">
  144 |                   l(word.language)
      |                         ^
  
  147 |                 </p>
  148 |               </div>
Error: Command "npx prisma generate && next build" exited with 1

0 Replies