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.

Make GetStaticProps and GetServerSideProps types use generic props

See original GitHub issue

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

github_iconTop GitHub Comments

19reactions
abhijithvijayancommented, Mar 25, 2020

What about types in context params?

in [id].ts

export const getServerSideProps: GetServerSideProps = async (context) => {
    const { id } = context.params;
}

I get this error

Property 'id' does not exist on type 'ParsedUrlQuery | undefined'.
    27 | 
    28 | export const getServerSideProps: GetServerSideProps = async (context) => {
  > 29 |     const { id } = context.params;

In VSCODE Screenshot_20200325_195818

11reactions
jarekscommented, Mar 30, 2021

You got to check first if context.params is not undefined before destructuring this valu

Under what circumstances context.params can be undefined in getServerSideProps?

Read more comments on GitHub >

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

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