Next.js: typescript interface about getInitialProps , how to solve it ?

Created on 16 May 2019  Â·  5Comments  Â·  Source: vercel/next.js

Question about Next.js

Questions should be posted on https://spectrum.chat/next-js
image

Most helpful comment

With the latest release of next, you can type your pages like so:

import { NextPage } from 'next'

const Homepage: NextPage<HomepageProps> = () => (
  <h1>Hello world</h1>
)

Homepage.getInitialProps = () => ({ ... })

All 5 comments

I think something like this should work:

const UserAuth: NextFC<IProps, { authInfo: ..., title: string, model: string }, NextContext> = ({ authInfo, ... }) => {
  return (
    <div>...</div>
  );
};
UserAuth.getInitialProps = async ({  }: NextContext) => {
  return {
    ...
  };
};

EDIT: this works assuming you are using @types/next. Otherwise you can check this PR for the next version of Next: https://github.com/zeit/next.js/pull/7337/files

thank you

hellow , i hava a other problem ,
image
If my first parameter has no value , This must be an empty object.
image
If removed , error

You have to have the three types, even if one of them is an empty object.

With the latest release of next, you can type your pages like so:

import { NextPage } from 'next'

const Homepage: NextPage<HomepageProps> = () => (
  <h1>Hello world</h1>
)

Homepage.getInitialProps = () => ({ ... })
Was this page helpful?
0 / 5 - 0 ratings