Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(typedef): AppType typedef changed in 12.3 #40371

Closed
1 task done
juliusmarminge opened this issue Sep 8, 2022 · 6 comments
Closed
1 task done

bug(typedef): AppType typedef changed in 12.3 #40371

juliusmarminge opened this issue Sep 8, 2022 · 6 comments
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.

Comments

@juliusmarminge
Copy link
Contributor

Verify canary release

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

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.15.1
      npm: 8.11.0
      Yarn: 1.22.19
      pnpm: 7.10.0
    Relevant packages:
      next: 12.3.0
      eslint-config-next: 12.3.0
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

When bumping version to Next 12.3.0 we ran into some troubles with the AppType typedefinition:

CleanShot 2022-09-08 at 22 30 41

The AppType typedefinition and specifically the pageProps seems to have been broken in 12.3.0 and the pageProps now evaluates to {}:
CleanShot 2022-09-08 at 22 34 22

Expected Behavior

I am not sure if this is a bug or if it's intended, but this change is not present in the changelog and seems to be a "breaking" one worth noting. Maybe we are required to augment some part to make it work with 3rd party dependencies?

Link to reproduction

https://github.com/juliusmarminge/apptype-repro

To Reproduce

Create a new next-app (using reproduction template):

npx create-next-app -e reproduction-template

Install NextAuth:

nom install next-auth

Create pages/_app.tsx with the following content:

import { SessionProvider } from "next-auth/react";
import type { AppType } from "next/dist/shared/lib/utils";

const MyApp: AppType = ({ Component, pageProps }) => {
  return (
    <SessionProvider session={pageProps.session}>
      <Component {...pageProps} />
    </SessionProvider>
  );
};

Hover pageProps, it evaluates to {} and the pageProps.session will error. This was not an issue in 12.2.5.

@juliusmarminge juliusmarminge added the bug Issue was opened via the bug report template. label Sep 8, 2022
@c-ehrlich
Copy link

Appears to be related to this typedef change

12.2:

export declare type AppInitialProps = {
    pageProps: any;
};
export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps & {
    Component: NextComponentType<NextPageContext, any, P>;
    router: R;
    __N_SSG?: boolean;
    __N_SSP?: boolean;
    __N_RSC?: boolean;
};

12.3:

export declare type AppInitialProps<P = any> = {
    pageProps: P;
};
export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps<P> & {
    Component: NextComponentType<NextPageContext, any, any>;
    router: R;
    __N_SSG?: boolean;
    __N_SSP?: boolean;
    __N_RSC?: boolean;
};

@juliusmarminge
Copy link
Contributor Author

Appears to be related to this typedef change

12.2:

export declare type AppInitialProps = {

    pageProps: any;

};

export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps & {

    Component: NextComponentType<NextPageContext, any, P>;

    router: R;

    __N_SSG?: boolean;

    __N_SSP?: boolean;

    __N_RSC?: boolean;

};

12.3:

export declare type AppInitialProps<P = any> = {

    pageProps: P;

};

export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps<P> & {

    Component: NextComponentType<NextPageContext, any, any>;

    router: R;

    __N_SSG?: boolean;

    __N_SSP?: boolean;

    __N_RSC?: boolean;

};

To be clear i think this is a very good improvement and seems like it would allow for a more typesafe usage in the root app.

Maybe the AppType should be a generic like NextPage, or React.FC and letting me pass in the props like

const MyApp: AppType<{ session: Session }> = ...

@balazsorban44
Copy link
Member

Hi, AppType is not exposed/documented (you are importing it from next/dist/shared/lib/utils), so this does not count as a bug. To type the props of App, you can do:

import type { AppProps } from "next/app"
import type { Session } from "next-auth"
import { SessionProvider } from "next-auth/react"

export default function MyApp(appProps: AppProps<{ session: Session }>) {
  const {
    Component,
    pageProps: { session, ...pageProps },
  } = appProps
  return (
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

@juliusmarminge
Copy link
Contributor Author

juliusmarminge commented Sep 9, 2022

Hi, AppType is not exposed/documented (you are importing it from next/dist/shared/lib/utils), so this does not count as a bug. To type the props of App, you can do:

import type { AppProps } from "next/app"
import type { Session } from "next-auth"
import { SessionProvider } from "next-auth/react"

export default function MyApp(appProps: AppProps<{ session: Session }>) {
  const {
    Component,
    pageProps: { session, ...pageProps },
  } = appProps
  return (
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

Thanks for the response. I've always thought the import path has been weird and could probably have guessed it wasn't for official usage.

However... typing arrow function components like const MyApp: AppType is very nice imo in that you get typesafety over the props and the return type at the same time.

Would you accept this as a feature request in exposing this type similar to the AppProps and allow it as a generic

import type { AppType } from "next/app";

const MyApp: AppType<{ session: Session }> = ({
  Component,
  pageProps
}) => { ... };

export default trpc.withTRPC(MyApp); // example

The alternative would be to just type the return type explicitly but not as clean imo

import type { AppProps } from "next/app";

const MyApp = (
  { Component, pageProps}: AppProps<{ session: Session }>
): JSX.Element => { ...}

@balazsorban44
Copy link
Member

Yes, it makes more sense as a feature request, I started working on it already. See #40391

ijjk pushed a commit that referenced this issue Sep 9, 2022
An alternative solution to #40371

Ref: #38867, t3-oss/create-t3-app#412,
t3-oss/create-t3-app#414

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants