[SOLVED] Nextjs how to type Error page with typescript

Issue

How can type an nextjs Error page with Typescript

I’ve tried with:

interface ErrorProps {
  statusCode: number;
}
function Error({ statusCode }: ErrorProps) {
  return (
    <p>
      {statusCode
        ? `An error ${statusCode} occurred on server`
        : "An error occurred on client"}
    </p>
  );
}

interface InitialProps {
  res: NextApiResponse;
  err: NextApiResponse;
}
Error.getInitialProps = ({ res, err }: InitialProps) => {
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404;
  return { statusCode };
};

but not sure it’s the right way

Solution

According to Next’s own documentation ->

pages/_error.js is only used in production. In development you’ll get an error with the call stack to know where the error originated from.

Answered By – Davi Lacerda

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *