I'm using the app router in Next.js 13 v13.1.1 and the layout.ts file is required. I'm using typescript, so I need to type the props of the RootLayout component. Does Next.js declare/export or otherwise imply the proptypes for the RootLayout
component?
For more context about my specific situation:
I'm using next-auth to create a social login, and the SessionProvider component requires the Next.js session to achieve this. With a bit of experimentation, I realized that RootLayout
exposes a managed session in the props, which the SessionProvider component can use to do all the cool next-auth things.
So naturally, I did this:
import { ReactNode } from 'react';
import { Session } from 'next-auth';
import SessionInitializer from './SessionInitializer';
interface RootLayoutProps {
children: ReactNode;
session: undefined | null | Session;
}
export default function RootLayout(props: RootLayoutProps) {
const { children, session } = props;
return (
<html>
<SessionInitializer session={session}>
<div>{ children }</div>
</SessionInitializer>
</html>
)
}
where SessionInitializer
is a simple client-side component, as so:
"use client";
import { SessionProvider } from 'next-auth';
interface SessionInitializerProps {
children: ReactNode;
session: undefined | null | Session;
}
export default function SessionInitializer(props: SessionInitializerProps) {
return (
<SessionProvider session={session}>
{ children }
</Session>
)
}
This works perfectly well on the next dev server, but won't build because my session isn't typed properly. In particular, the linting stage complains that the RootLayout component I'm exporting from layout.ts isn't the correct type.
Here's a simplified form of my exact error:
...
app/layout.tsx
Type error: Layout "app/layout.tsx" has an invalid "default" export:
Type "RootLayoutProps" is not valid.
ELIFECYCLE Command failed with exit code 1.
ERROR: command finished with error: command (absolute-path-to-root-of-next-project) pnpm run build exited (1)
...
So yeah, can anyone point me to a document or source-code that can assist me in typing the RootLayout component's props, or otherwise suggest a better way of typing my component? I already know that my RootLayoutProps can't possibly be correct, given that I'm typing it using a type from a 'random' 3rd party library.
Thanks in advance for the help, friend.