Skip to content

fix: correct AppType props type usage - #98980

Open
snmsoodan wants to merge 1 commit into
vercel:canaryfrom
snmsoodan:fix/apptype-props
Open

snmsoodan wants to merge 1 commit into
vercel:canaryfrom
snmsoodan:fix/apptype-props

Conversation

@snmsoodan

Copy link
Copy Markdown

Fix AppType generic being applied to pageProps instead of props

Closes #42846

Summary

The generic parameter P on AppType<P> was being applied to the pageProps
property of the App component's props instead of to the top-level props.

For example, with:

type MyInitialProps = { foo: string }

const MyApp: AppType<MyInitialProps> = ({ Component, pageProps, foo }) => {
  // ...
}

TypeScript reported Property 'foo' does not exist on type 'AppPropsType<any, MyInitialProps>'
for props.foo (even though foo exists at runtime), while props.pageProps.foo
was incorrectly allowed (even though foo is not on pageProps at runtime).

This happened because AppType<P> was defined as:

export type AppType<P = {}> = NextComponentType<
  AppContextType,
  P,
  AppPropsType<any, P> // <-- P was used as the pageProps type
>

Change

Apply the generic P to the top-level props instead of to pageProps:

export type AppType<P = {}> = NextComponentType<
  AppContextType,
  P,
  AppPropsType<any, any> & P // <-- P is merged into the App's props
>

Now props.foo is correctly typed (from P), and pageProps remains a separate
property of props (typed any, since AppType cannot know the page's props).
If a user wants to type pageProps, they can include it in P.

Why this is correct

  • App.getInitialProps populates the top-level props of the App component,
    not pageProps (only a page's getStaticProps/getServerSideProps/
    getInitialProps populate pageProps).
  • pageProps should remain a property of props, per maintainer guidance.
  • The change is scoped to AppType only, so AppProps<P> (the type most _app
    files use, where P is the pageProps type) is completely unaffected.

Verification

  • Reproduced the reported error with the current definition.
  • Confirmed the fixed definition compiles with foo on props and pageProps
    as a separate property.
  • Confirmed the existing test/e2e/typescript/pages/_app.tsx usage
    (AppType<{ foo: string }> with { Component, pageProps }) still compiles.
  • Confirmed AppProps<P> behavior is unchanged.

Files changed

  • packages/next/src/shared/lib/utils.ts

Note: This PR was prepared with the assistance of GitHub Copilot (an AI
coding agent). The change and verification were produced autonomously and
reviewed before submission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parameter on AppType is used incorrectly

1 participant