Make GetStaticProps and GetServerSideProps types use generic props
See original GitHub issueFeature request
The GetStaticProps and GetServerSideProps typescript types allow us to use the new data fetching methods with typescript, but there is no type check for props content.
Is your feature request related to a problem? Please describe.
The current implementation is as follows:
export type GetStaticProps = (ctx: {
params?: ParsedUrlQuery
preview?: boolean
previewData?: any
}) => Promise<{
props: { [key: string]: any }
revalidate?: number | boolean
}>
Describe the solution you’d like
export type GetStaticProps<T = {}> = (ctx: {
params?: ParsedUrlQuery
preview?: boolean
previewData?: any
}) => Promise<{
props: T
revalidate?: number | boolean
}>
This way, when defining a Props interface in our page, we can have type safety:
interface Props {
name: string
}
const Page: NextPage<Props> = (props) => {
return <div>{props.name}</div>
}
export const getStaticProps: GetStaticProps<Props> = async (context) => {
const name = await getName(); // string
return {
props: {
name, // yay! Type checked props
},
};
};
Something very similar would be implemented for GetServerSideProps.
Describe alternatives you’ve considered
As the new methods are not static properties of pages anymore, we cannot infer them via the page, so the user may need to add the generic Props type parameter in many places.
Perhaps the types could have more parameters to allow typing previewData?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
How to make Next.js getStaticProps work with typescript
This will autogenerate a type from GetStaticProps to your component it ... even better would be to improve typing with the Props generic:...
Read more >Using getStaticProps and getStaticPaths with TypeScript
I'm using the variable name slug to create the dynamic routes, but you could use anything - another common name is id ....
Read more >How to use GetStaticProps and GetStaticPaths with TypeScript
Use the NextPage , GetStaticProps and GetStaticPaths types provided by Next.js; Provide generic types to NextPage , GetStaticProps and ...
Read more >How To Automatically Get Props Types in Next.js and TypeScript
These will automatically infer the types for your page component props coming from the getServerSideProps() or getStaticProps() function ...
Read more >Next.js + TypeScript: Tips and FAQs for Every Stage of Your ...
Use `create-next-app` with the TypeScript flag to start from scratch ... generic types to NextPage , GetStaticProps , and GetStaticPaths .
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

What about types in context params?
in
[id].tsI get this error
In VSCODE
Under what circumstances
context.paramscan be undefined in getServerSideProps?