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

(Docs) getStaticProps & getServerSideProps TS section (#40607) #40639

Merged
merged 4 commits into from Oct 1, 2022

Conversation

sicikh
Copy link
Contributor

@sicikh sicikh commented Sep 17, 2022

Added examples to getStaticProps & getServerSideProps explicit and implicit typings for TypeScript.

Related issues: fixes #40607

@HaNdTriX
Copy link
Contributor

HaNdTriX commented Sep 18, 2022

Hey @sicikh thank you for working on improving the documentation 🙏.

From my point of view I don't recommend mixing the type GetServerSideProps and InferGetServerSidePropsType.
This is because the type GetServerSideProps explicitly tells typescript what getStaticProps will return. InferGetServerSidePropsType will infer these types instead of defining them. If you now use both InferGetServerSidePropsType will get the type from GetServerSideProps<T> instead of inferring it from js.

Screenshot 2022-09-18 at 20 49 01

Screenshot 2022-09-18 at 20 49 29

As you can see the last approach infers the correct types the first one uses a default type which is critical from my point of view.

The same applies to GetStaticProps. I am currently working on fixing InferGetServerSidePropsType types in #40635.

@sicikh
Copy link
Contributor Author

sicikh commented Sep 19, 2022

Hey @sicikh thank you for working on improving the documentation 🙏.

From my point of view I don't recommend mixing the type GetServerSideProps and InferGetServerSidePropsType. This is because the type GetServerSideProps explicitly tells typescript what getStaticProps will return. InferGetServerSidePropsType will infer these types instead of defining them. If you now use both InferGetServerSidePropsType will get the type from GetServerSideProps<T> instead of inferring it from js.

Screenshot 2022-09-18 at 20 49 01

Screenshot 2022-09-18 at 20 49 29

As you can see the last approach infers the correct types the first one uses a default type which is critical from my point of view.

The same applies to GetStaticProps. I am currently working on fixing InferGetServerSidePropsType types in #40635.

Thanks for the reply! The problem given in your example is exactly what was missing in the existing documentation. The current behavior of types when we explicitly specify the type of functions is the standard behavior of TypeScript with type calculation. If we want to type the props in the page, while maintaining the explicit typing of getStaticProps, then we must give the types of the returned props in the generics of the function getStaticProps: GetStaticProps<{ propsObject: propsObjectType }>. Otherwise, InferGetStaticProps will type the props as [key:string]: any because of the following:

image

That is why it is necessary to specify two examples in the documentation, with an explicit and implicit specification of the type of function of data fetching.

Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kodiakhq kodiakhq bot merged commit c988b99 into vercel:canary Oct 1, 2022
}
```

If you want to get inferred typings for your props, you can use `InferGetStaticPropsType<typeof getStaticProps>`:

```tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will tsx work? I am unsure (worth checking)

BowlingX pushed a commit to BowlingX/next.js that referenced this pull request Oct 5, 2022
…ercel#40639)

Added examples to `getStaticProps` & `getServerSideProps` explicit and implicit typings for TypeScript.

Related issues: fixes vercel#40607

Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
Kikobeats pushed a commit to Kikobeats/next.js that referenced this pull request Oct 24, 2022
…ercel#40639)

Added examples to `getStaticProps` & `getServerSideProps` explicit and implicit typings for TypeScript.

Related issues: fixes vercel#40607

Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs: getStaticProps & InferGetStaticPropsType | TypeScript
6 participants