61
loading...
This website collects cookies to deliver better user experience
// pages/_app.tsx
export default function MyApp({ Component, pageProps }) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout || ((page) => page)
return getLayout(<Component {...pageProps} />)
}
Parameter 'page' implicitly has an 'any' type.
Property 'getLayout' does not exist on type 'NextComponentType<NextPageContext, any, {}>'.
Property 'getLayout' does not exist on type 'ComponentClass<{}, any> & { getInitialProps?(context: NextPageContext): any; }'
import { ReactNode } from 'react';
// pages/_app.tsx
export default function MyApp({ Component, pageProps }) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout || ((page: ReactNode) => page)
return getLayout(<Component {...pageProps} />)
}
Component
doesn't include getLayout
function. We need to declare a new types. Let's create a next.d.ts
file somewhere in the project, with the following content:// next.d.ts
import type {
NextComponentType,
NextPageContext,
NextLayoutComponentType,
} from 'next';
import type { AppProps } from 'next/app';
declare module 'next' {
type NextLayoutComponentType<P = {}> = NextComponentType<
NextPageContext,
any,
P
> & {
getLayout?: (page: ReactNode) => ReactNode;
};
}
declare module 'next/app' {
type AppLayoutProps<P = {}> = AppProps & {
Component: NextLayoutComponentType;
};
}
NextLayoutComponentType
and AppLayoutProps
that we can use in place of original types. Our initial code will need to be changed to this:// pages/_app.tsx
import { AppContext, AppInitialProps, AppLayoutProps } from 'next/app';
import type { NextComponentType } from 'next';
import { ReactNode } from 'react';
const MyApp: NextComponentType<AppContext, AppInitialProps, AppLayoutProps> = ({
Component,
pageProps,
}: AppLayoutProps) => {
const getLayout = Component.getLayout || ((page: ReactNode) => page);
return getLayout(<Component {...pageProps} />);
};
export default MyApp;
AppLayoutProps
. It includes the other custom type for Component
that now contains getLayout
function.