Extending a TypeScript Type with an optional property

Viewed 3793

I am working with NextJS and need to extend their AppProps type with an optional parameter that I have added myself Layout. However the AppProps type is made up of many others e.g.

AppProps:

export declare type AppProps<P = {}> = AppPropsType<Router, P>;

AppPropsType:

export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps & {
    Component: NextComponentType<NextPageContext, any, P>;
    router: R;
    __N_SSG?: boolean;
    __N_SSP?: boolean;
};

NextComponentType:

export declare type NextComponentType<C extends BaseContext = NextPageContext, IP = {}, P = {}> = ComponentType<P> & {
    /**
     * Used for initial page load data population. Data returned from `getInitialProps` is serialized when server rendered.
     * Make sure to return plain `Object` without using `Date`, `Map`, `Set`.
     * @param ctx Context of `page`
     */
    getInitialProps?(context: C): IP | Promise<IP>;
};

From my _app.tsx I want to be able to add Layout to Component with something like:

  Component: {
    Layout?: React.FunctionComponent;
  };

Is there a way to do this from AppProps or would I need to extend/intersect AppPropsType first with Layout then redeclare my own AppProps which uses the extended AppPropsType?

4 Answers

At first, I had this ugly mess to stop errors:

import { AppProps } from 'next/app'    

export default function App({ Component, pageProps }: AppProps) {
  const RedeclaredAndHacky_Component = Component as any
  const Layout = RedeclaredAndHacky_Component.layoutProps?.Layout || Fr
  /* Rest of the function */
}

I didn't really like this, so I ended up with something closer to what you described:

import { NextComponentType, NextPageContext } from 'next'

type AppProps = {
  pageProps: any
  Component: NextComponentType<NextPageContext, any, {}> & { layoutProps: any }
}

export default function App({ Component, pageProps }: AppProps) {
  const Layout = Component.layoutProps?.Layout || Shell
  /* Rest of the function */
}

I create a custom AppProps Next type so that way I can edit the Component type.

interface CustomAppProps extends Omit<AppProps, "Component"> {
  Component: AppProps["Component"] & { Layout: JSX.Element };
}

The final result will look something like this:

import type { AppProps } from "next/app";

interface CustomAppProps extends Omit<AppProps, "Component"> {
  Component: AppProps["Component"] & { Layout: JSX.Element };
}

function CustomApp({ Component, pageProps }: CustomAppProps) {
  return <Component {...pageProps} />;
}

export default CustomApp;

In the material-ui examples there is some code which may help readers of this question as well: https://github.com/mui/material-ui/blob/3c528113f47cfddf2504ea02ba3065e7c5c46928/examples/nextjs-with-typescript/pages/_app.tsx#L13-L15

import * as React from 'react';
import Head from 'next/head';
import { AppProps } from 'next/app';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider, EmotionCache } from '@emotion/react';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

interface MyAppProps extends AppProps {
  emotionCache?: EmotionCache;
}

export default function MyApp(props: MyAppProps) {
  const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
  return (
    <CacheProvider value={emotionCache}>
      <Head>
        <meta name="viewport" content="initial-scale=1, width=device-width" />
      </Head>
      <ThemeProvider theme={theme}>
        {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
        <CssBaseline />
        <Component {...pageProps} />
      </ThemeProvider>
    </CacheProvider>
  );
}

I've modified a little bit the answer of Pablo Verduzco. I think is more readable and extensible.

import type { AppProps } from "next/app"

interface CustomAppProps {
  Component: AppProps["Component"] & {
    auth: {
      role: string
    }
  }
  pageProps: AppProps["pageProps"]
}

function MyApp({ Component, pageProps }: CustomAppProps) {
  return <Component {...pageProps} />;
}

export default MyApp;
Related