You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created some utility types which I think would be nice to have on the TypeScript integration page. I don't think the package itself should export all these, because the types depends on configuration of the user. Locale segment in pages can be handled multiple ways and the type can be narrowed from string if need be and Next 15+ uses promises.
These are for passing around translations (useful for lists with dynamic translations, you don't have to load translations in every child):
// import type needs TypeScript 3.8+importtype{NamespaceKeys,NestedKeyOf,useTranslations}from"next-intl";importtype{getTranslations}from"next-intl/server";/** * Type for passing around NextIntl getTranslation or useTranslations functions */exporttypegetTranslationsType<TextendsNamespaceKeys<IntlMessages,NestedKeyOf<IntlMessages>>,>=|Awaited<ReturnType<typeofgetTranslations<T>>>|ReturnType<typeofuseTranslations<T>>;/** * Type for passing around keys for given getTranslation or useTranslations functions */exporttypegetTranslationsKeys<TextendsNamespaceKeys<IntlMessages,NestedKeyOf<IntlMessages>>,>=Parameters<getTranslationsType<T>>[0];
Usage:
// messages/en.json{"hello": {"world": "Hello world!"}}// getTranslationsTypefunctionMyList(){constt=getTranslations("hello");// or useTranslations("hello") in client components.return(<ul>{items.map(item=>(<MyListItemt={t}>))}</ul>);}functionMyListItem(props: {t: getTranslationsType<"hello">}){return(<li>{props.t("world")} // Autocomplete works here for keys.
</li>);}// getTranslationsKeysfunctionMyComponent(props: {key: getTranslationsKeys<"hello">}){constt=getTranslations("hello");// or useTranslations("hello") in client components.return(<h1>{t(props.key)}</h1>);}
For Next.js 15+ a type for page and layout props which forces the ussage of promises. I don't know if these type interferes with Dynamic IO, I assume the Next compiler uses the actual code, not the TypeScript types given by the user.
importtype{PropsWithChildren}from"react";/** * Type for page props that already contains "locale" segment. */exporttypeLocalePageProps<T=unknown>={readonlyparams: Promise<{/** * Segment that determines the locale. */readonlylocale: string;}>;}&Readonly<T>;/** * Type for layout props that already contains "locale" segment and "children". */exporttypeLocaleLayoutProps<T=unknown>=PropsWithChildren<LocalePageProps<T>>;
Usage:
// page.tsxexportdefaultasyncfunctionPage(props: LocalePageProps){setRequestLocale((awaitprops.params).locale);// Rest of the page}// [id]/page.tsxexportdefaultasyncfunctionPage(props: LocalePageProps<{// searchParams, or other Next page props can be defined here too.params: {id: string// string | readonly string[] | undefined would be a safer option}}>){constparams=awaitprops.paramssetRequestLocale(params.locale);constdata=getData(params.id);// Rest of the page}// layout.tsxexportdefaultasyncfunctionLayout(props: LocaleLayoutProps){// Enable static renderingsetRequestLocale((awaitprops.params).locale);returnprops.children;}
The text was updated successfully, but these errors were encountered:
Thank you for carefully writing down your thoughts!
The question about typing the return type of useTranslations comes up from time to time, but I'm hesitant to recommend passing t to a function since it might not work with future optimizations like #1. My recommendation is to never pass t to a function and instead use it as returned from useTranslations/getTranslations.
Regarding LocalePageProps, I think once rootParams lands, you can avoid reading locale from params at all and just use useLocale()/getLocale() where relevant. For the time being, I think this type can be useful though, I'm using it myself in projects! 🙂
I created some utility types which I think would be nice to have on the TypeScript integration page. I don't think the package itself should export all these, because the types depends on configuration of the user. Locale segment in pages can be handled multiple ways and the type can be narrowed from string if need be and Next 15+ uses promises.
These are for passing around translations (useful for lists with dynamic translations, you don't have to load translations in every child):
Usage:
For Next.js 15+ a type for page and layout props which forces the ussage of promises. I don't know if these type interferes with Dynamic IO, I assume the Next compiler uses the actual code, not the TypeScript types given by the user.
Usage:
The text was updated successfully, but these errors were encountered: