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.

Type GetStaticProps needs some tinker

See original GitHub issue

Describe the feature you’d like to request

Summary

next.GetStaticProps stop extends return type to Record<string,any>

Detailed

Say I have a getStaticProps like this.

const getStaticProps = async () => {
  const typedProps = {
    str: "stringType",
    num: 123,
  }
  return {
    props: typedProps,
  };
};

Typescript knows the type of typedProps

const typedProps: {
    str: string;
    num: number;
}

And the InferGetStaticPropsType<typeof getStaticProps> in the same page works correctly.

However, after adding the type GetStaticProps to the getStaticProps. the const SomePage: NextPage<InferGetStaticPropsType<typeof getStaticProps>> starts to accept {[key: string]: any} as its argument and all type information of the props is lost (converted to any).

Describe the solution you’d like

I want GetStaticProps<P,Q,D> stop to have default value of P = { [key: string]: any } from https://github.com/vercel/next.js/blob/ccd3df2f812c98874273de7b237baa9c2ad8f309/packages/next/types/index.d.ts#L118-L124

But to me this is limited by TS engine, so I don’t know how to fix it. (otherwise I’d make a PR)

Describe alternatives you’ve considered

Not use GetStaticProps. Deprecate this type

Related issues

#32434 , #11328

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:15

github_iconTop GitHub Comments

1reaction
GabenGarcommented, Jun 3, 2022

Yeah that would be a better approach but also will not make people happy when their builds suddenly start to fail and the only solution to this is declaring props interfaces on potentially 100s of page components.

1reaction
MonstraGcommented, Jun 3, 2022

That works, but then he needs to type page and type props separately

What I ment is that you do

type Props = {
    data: Thingie
}

const Page: NextPage<Props> = (props) => (

...

export const getServerSideProps: GetServerSideProps = async (ctx): Promise<GetServerSidePropsResult<Props>> => {

So you declare Props once, but you use it twice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

i18n with next export calls getStaticProps for each ... - GitHub
I'm attempting to use the brand new i18n feature for a static exported site. During build time it calls getStaticProps once for each...
Read more >
Data Fetching: getStaticProps - Next.js
Learn how to use `getStaticProps` to generate static pages with Next.js. ... In some rare cases, you might need to assign a custom...
Read more >
How to use GetStaticProps and GetStaticPaths with TypeScript
Use the NextPage , GetStaticProps and GetStaticPaths types provided by ... You can pass the type of props the page expect, as a...
Read more >
How to make Next.js getStaticProps work with typescript
This will autogenerate a type from GetStaticProps to your component it ... It addressed the issue where getStaticPaths requires a type of ...
Read more >
Using getStaticProps and getStaticPaths with TypeScript
js GitHub regarding this issue, I recognised it was a gap in their documentation. It explains how to add the type to getStaticProps...
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