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

InferGetStaticPropsType does not infer props when GetStaticProps is used #29963

Closed
selbekk opened this issue Oct 16, 2021 · 4 comments
Closed
Labels
bug Issue was opened via the bug report template.

Comments

@selbekk
Copy link

selbekk commented Oct 16, 2021

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

16.9.1

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

Props are not being inferred correctly by InferGetStaticPropsType in the following case:

export const getStaticProps: GetStaticProps = (context) => {
  return {
    props: { hello: 'world' }
  }
}

export default function Example({ hello }: InferGetStaticPropsType<typeof getStaticProps>) {
  // here, `hello` is "any"
}

If I remove the GetStaticProps definition on the first line, things work as expected. I assume that's because I could specify the props type as a type argument to GetStaticProps<{ hello: string }>, but I don't want that.

Is this even possible to fix?

Expected Behavior

Would love for this to just propagate the props as expected.

To Reproduce

Create a new Next.js project with TypeScript, and create a new pages file (index.tsx) for example. Paste the following code:

export const getStaticProps: GetStaticProps = (context) => {
  return {
    props: { hello: 'world' }
  }
}

export default function Example({ hello }: InferGetStaticPropsType<typeof getStaticProps>) {
  // here, `hello` is "any"
}
@selbekk selbekk added the bug Issue was opened via the bug report template. label Oct 16, 2021
@ephraimduncan
Copy link
Contributor

ephraimduncan commented Oct 22, 2021

Don't forget you can obtain argument type in getStaticProps with GetStaticPropsContext.

import { InferGetStaticPropsType, GetStaticPropsContext} from 'next'

export const getStaticProps = (context: GetStaticPropsContext) => {
  return {
    props: { hello: 'world' }
  }
}

export default function Example({ hello }: InferGetStaticPropsType<typeof getStaticProps>) {
}

export default Blog

@selbekk
Copy link
Author

selbekk commented Oct 22, 2021

That's true – and what I ended up doing as a workaround – but I prefer using the function type, so I don't have to specify both the arguments and the return values separately

@timneutkens
Copy link
Member

Going to close this as a duplicate of #15913 to track it in one place.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 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.
Projects
None yet
Development

No branches or pull requests

4 participants