Next.js + next-intl构建时children隐式any类型错误求助
解决Root Layout中children隐式any类型错误
这个错误是TypeScript类型检查触发的——你没给children参数指定明确类型,TS默认将其推断为any,而Next.js构建时的严格类型检查会抛出这个报错。
直接给children加上类型注解就能解决,推荐用Next.js官方提供的LayoutProps类型:
import type { LayoutProps } from 'next/app'; export default function RootLayout({ children }: LayoutProps) { return children; }
如果后续要在RootLayout里添加next-intl的国际化包裹(比如IntlProvider),可以参考这个结构:
import type { LayoutProps } from 'next/app'; import { NextIntlClientProvider } from 'next-intl'; import { getMessages } from 'next-intl/server'; export default async function RootLayout({ children, params }: LayoutProps) { const messages = await getMessages({ locale: params.locale }); return ( <html lang={params.locale}> <body> <NextIntlClientProvider messages={messages}> {children} </NextIntlClientProvider> </body> </html> ); }
另外,也可以直接用React的ReactNode类型定义children,效果一样:
import type { ReactNode } from 'react'; export default function RootLayout({ children }: { children: ReactNode }) { return children; }
内容的提问来源于stack exchange,提问作者PetrP
相关产品推荐
相关产品推荐

