next.js: Parameter on `AppType` is used incorrectly

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

environment information
sandbox@sse-sandbox-xhvisb:/sandbox$ yarn next info
yarn run v1.22.19
$ /sandbox/node_modules/.bin/next info

    Operating System:
      Platform: linux
      Arch: x64
      Version: #58~20.04.1-Ubuntu SMP Tue Jun 14 11:29:12 UTC 2022
    Binaries:
      Node: 14.19.3
      npm: 6.14.17
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.0.3-canary.4
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0

warn  - Latest canary version not detected, detected: "13.0.3-canary.4", newest: "13.0.3".
        Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
        Read more - https://nextjs.org/docs/messages/opening-an-issue
Done in 0.84s.

Note: warning at the end seems to be because I’m using next@canary (as instructed) but canary version is older than latest official release. My issue is present in either version.

Describe the Bug

Prop types defined by the parameter on AppType are incorrectly applied to MyApp’s props.pageProps instead of MyApp’s props.

Expected Behavior

Expected TS types to correctly reflect runtime types.

Link to reproduction

https://codesandbox.io/s/polished-tree-xhvisb?file=/pages/_app.tsx

To Reproduce

  1. Open the link to reproduction
  2. Observe code on left (with TS errors) and the produced page on right.

There’s a TS error on line 13, complaining Property 'foo' does not exist on type 'AppPropsType<any, MyInitialProps>', even though props.foo does exist at runtime.

Also, there’s no TS error on line 14, even though props.pageProps.foo does not exist at runtime.

image

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 17 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Hey! I’d like to take this one.

There is a slight runtime difference between App.getInitialProps and any page’s getStaticProps, getServerSideProps and getInitialProps behavior. The latter 3 will populate pageProps, while App.getInitialProps will not.

For: #42846 Hi, to fix this issue we need to add any type in function parameter, it will fix the issue.

@RonakShishangia Using any is not a solution. I want to use types. Using any just works around the problem by not using types.

For: https://github.com/vercel/next.js/issues/42846 Hi, to fix this issue we need to add any type in function parameter, it will fix the issue.

const MyApp: AppType<MyInitialProps> = (props: any) => {
  return (
    <div>
      <props.Component {...props.pageProps} />
      <hr />
      <p>{`props.foo: ${props.foo}`}</p>
      <p>{`props.pageProps.foo: ${props.pageProps.foo}`}</p>
    </div>
  );
};

MyApp.getInitialProps = async (ctx: any) => {
  const props = await App.getInitialProps(ctx); // <--another separate issue
  return {
    ...props,
    foo: "bar" // TS complains MyApp type is wrong if this line is excluded
  };
};

image

Hi Guys, is this issue fixed? I want to get started with open source contributions so wanted to pick this task.

I don’t don’t think that’s needed. pageProps should be a property of props.