You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 18:14:55