Infer types of props returned by getStaticProps
See original GitHub issueFeature request
Right now I have to redeclare the types for page props injected by getStaticProps, although typescript already infers them correctly. We could create a type that infers them from the locally exported typeof getStaticProps.
Describe alternatives you’ve considered
I am by no means a TS expert, so I am not sure if there are better alternatives for inferring the types.
First working draft:
export type StaticPropsReturnValue<P> = {
props: P;
revalidate?: number | boolean;
};
export type ResolvedGetStaticProps<T extends (...args: any) => any> =
T extends (...args: any) => Promise<StaticPropsReturnValue<infer U>>
? StaticPropsReturnValue<U> : never;
export type StaticProps<T extends GetStaticProps> = ResolvedGetStaticProps<T>["props"];
used like this:
export const getStaticProps = async () => ({
props: {
propNumberOne: "hello world",
propNumberTwo: 2,
},
});
const TypesTest = ({ propNumberOne }: StaticProps<typeof getStaticProps>) => (<div>{propNumberOne}</div>);
we get correct type inference for our props.
Next steps:
- I would love to get feedback from TS experts if there is a better solution to do this
- if you like the feature I will gladly create a PR for this.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
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 >How to make Next.js getStaticProps work with typescript
Try this: export const getStaticProps: GetStaticProps = async () => { // must be async return { props: { host: process. env. DB_HOST....
Read more >infer-next-props-type - npm
Start using infer-next-props-type in your project by running `npm i ... export function getStaticProps() { return { props: { foo: 'bar' } ...
Read more >Using getStaticProps and getStaticPaths with TypeScript
It explains how to add the type to getStaticProps when used on its ... was created in getStaticPaths and returned inside the params...
Read more >Use TypeScript with Next.js - Documentation - Prismic
Page components can be typed using the inferred type from the page's getStaticProps() or getServerSideProps() function. Next.js provides InferGetStaticPropsType ...
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

causes
props: neverhowever the following works fine:and so does:
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.