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 & InferGetStaticPropsType | TypeScript #40607

Closed
sicikh opened this issue Sep 16, 2022 · 4 comments · Fixed by #40639
Closed

Docs: getStaticProps & InferGetStaticPropsType | TypeScript #40607

sicikh opened this issue Sep 16, 2022 · 4 comments · Fixed by #40639
Labels
good first issue Easy to fix issues, good for newcomers

Comments

@sicikh
Copy link
Contributor

sicikh commented Sep 16, 2022

What is the improvement or update you wish to see?

image

In the documentation, it is advised to type getStaticProps first, and then in the example with InferGetStaticPropsType, getStaticProps is not typed. The documentation should specify this type behavior explicitly. All examples of type behavior are listed in the examples below.

The same with the other rendering methods

Is there any context that might help us understand?

Example 1. InferGetStaticPropsType is working as expected. Object posts in page Posts is type types.Post[].

const Posts = ({ posts }: InferGetStaticPropsType<typeof getStaticProps>) => {
    ...
};

export const getStaticProps: GetStaticProps<{ posts: types.Post[] }> = async () => {
    let data: {
        allPosts: types.Post[];
    };

   // fetching data

    const posts = data.allPosts;
    return {
        props: {
            posts,
        },
    };
};

Example 2. The same as in the previous example, but the getStaticProps type is specified implicitly. This example is listed in the documentation.

const Posts = ({ posts }: InferGetStaticPropsType<typeof getStaticProps>) => {
    ...
};

export const getStaticProps = async () => {
    let data: {
        allPosts: types.Post[];
    };

   // fetching data

    const posts = data.allPosts;
    return {
        props: {
            posts,
        },
    };
};

Example 3. InferGetStaticPropsType is not working. Object posts in page Posts is type any.

const Posts = ({ posts }: InferGetStaticPropsType<typeof getStaticProps>) => {
    ...
};

export const getStaticProps: GetStaticProps = async () => {
    let data: {
        allPosts: types.Post[];
    };

   // fetching data

    const posts = data.allPosts;
    return {
        props: {
            posts,
        },
    };
};

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/api-reference/data-fetching/get-static-props#getstaticprops-with-typescript

@sicikh sicikh added the Documentation Related to Next.js' official documentation. label Sep 16, 2022
@sicikh sicikh changed the title Docs: Docs: getStaticProps & InferGetStaticPropsType Sep 16, 2022
@sicikh sicikh changed the title Docs: getStaticProps & InferGetStaticPropsType Docs: getStaticProps & InferGetStaticPropsType | TypeScript Sep 16, 2022
@balazsorban44 balazsorban44 added good first issue Easy to fix issues, good for newcomers area: documentation and removed Documentation Related to Next.js' official documentation. labels Sep 16, 2022
@balazsorban44
Copy link
Member

Thanks for pointing this out, want to open a PR?

Here is the file that needs to be edited: https://github.com/vercel/next.js/blob/canary/docs/api-reference/data-fetching/get-static-props.md

@sicikh
Copy link
Contributor Author

sicikh commented Sep 16, 2022

Thanks for pointing this out, want to open a PR?

Here is the file that needs to be edited: https://github.com/vercel/next.js/blob/canary/docs/api-reference/data-fetching/get-static-props.md

Thank you for the answer and for specifying a documentation file, the link to the PR will be there tomorrow.

@sicikh
Copy link
Contributor Author

sicikh commented Sep 17, 2022

Thanks for pointing this out, want to open a PR?

Here is the file that needs to be edited: https://github.com/vercel/next.js/blob/canary/docs/api-reference/data-fetching/get-static-props.md

Here is a PR: #40639

@kodiakhq kodiakhq bot closed this as completed in #40639 Oct 1, 2022
kodiakhq bot pushed a commit that referenced this issue Oct 1, 2022
Added examples to `getStaticProps` & `getServerSideProps` explicit and implicit typings for TypeScript.

Related issues: fixes #40607

Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
BowlingX pushed a commit to BowlingX/next.js that referenced this issue 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 issue 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
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 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants