Profile picture

Software Engineer (Open Source, Dev Tools) at BoxyHQ.

Follow me on Twitter

Next.js - Infer SSR Props

August 10, 2022

type GetSSRResult<TProps> =
  | { props: TProps }
  | { redirect: any }
  | { notFound: boolean };

type GetSSRFn<TProps> = (args: any) => Promise<GetSSRResult<TProps>>;

export type inferSSRProps<TFn extends GetSSRFn<any>> = TFn extends GetSSRFn<infer TProps>
  ? NonNullable<TProps>
  : never;

const Members: NextPageWithLayout<inferSSRProps<typeof getServerSideProps>> = ({
  posts,
}) => {
  return <></>
}

export const getServerSideProps = async (
  context: GetServerSidePropsContext
) => {
  const posts = await prisma.post.findMany()

  return {
    props: {
      posts,
    },
  }
}

export default Members

Source


Kiran K