question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infer types of props returned by getStaticProps

See original GitHub issue

Feature 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lukebartoncommented, Aug 5, 2020
export const getServerSideProps = async ({ params }) => {
  return { 
    props: { foo: "bar" } 
  }
};

export const Page = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => { ... }

causes props: never however the following works fine:

export const getServerSideProps = async ({ params }: GetServerSidePropsContext) => {
  return { 
    props: { foo: "bar" } 
  }
};

export const Page = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => { ... }

and so does:

export const getServerSideProps = async (ctx) => {
  const params = ctx.params;
  return { 
    props: { foo: "bar" } 
  }
};

export const Page = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => { ... }
0reactions
balazsorban44commented, Jan 29, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found