-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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 always returns { [key: string]: any; } #32434
Comments
your code sample says |
Sorry that was a copy paste error :) |
This is because you added See the docs: https://nextjs.org/docs/basic-features/data-fetching#typescript-use-getstaticprops You want to let the return value be inferrable. If you need to type the params, you can use |
@balazsorban44 thank you! for future readers, this is what I changed to get things to work: const Home: NextPage<InferGetStaticPropsType<typeof getStaticProps>> = ({
posts
})
return (<div>hi</div>)
export const getStaticProps: GetStaticProps<{
posts: {
slug: string
title: string
summary: string
date: string
}[]
}> = async () => {
const posts = allArticles.map((post) =>
pick(post, ['slug', 'title', 'summary', 'date'])
)
return { props: { posts: posts } }
} |
Hm. It seems like sometimes the verbosity of all of those type declarations aren't required for resolution. For instance, the following works as is: import { InferGetStaticPropsType, NextPage } from 'next'
import { allPages } from '.contentlayer/data'
const Home: NextPage<InferGetStaticPropsType<typeof getStaticProps>> = ({
content
}) => {
// typeof content = Page | undefined (which is correct)
return (<div>foo</div>)
}
export const getStaticProps = async () => {
const content = allPages.find((page) => page.slug === 'index')
return { props: { content } }
}
export default Home |
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. |
What version of Next.js are you using?
12.0.7
What version of Node.js are you using?
17.2
What browser are you using?
Chrome latest
What operating system are you using?
macOS
How are you deploying your application?
next dev
Describe the Bug
See below:
Expected Behavior
Inside the getStaticProps function, I am able to correctly use the type of
posts
But inside of the main function, that doesn't work.
To Reproduce
The text was updated successfully, but these errors were encountered: