Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct type inference when using fallback: true #18705

Open
tomdohnal opened this issue Nov 2, 2020 · 2 comments
Open

Correct type inference when using fallback: true #18705

tomdohnal opened this issue Nov 2, 2020 · 2 comments
Labels
TypeScript Related to types with Next.js.
Milestone

Comments

@tomdohnal
Copy link
Contributor

Feature request

Is your feature request related to a problem? Please describe.

When I'm using fallback: true in getStaticPaths and using InferGetStaticPropsType to infer the props, it doesn't take the fact that the fallback page might load into account. It assumes that the props will always get passed to the page.

import { InferGetStaticPropsType } from 'next'

type Post = {
  author: string
  content: string
}

export const getStaticPaths = async ({params}) => {
  const res = await fetch(`https://.../posts/${params.id}`)
  const posts: Post[] = await res.json()

  return {
    paths: [{ params: { id: '1' }],
    fallback: true
  }
}

export const getStaticProps = async ({params}) => {
  const res = await fetch(`https://.../posts/${params.id}`)
  const posts: Post[] = await res.json()

  return {
    props: {
      posts,
    },
  }
}

function Blog({ posts }: InferGetStaticPropsType<typeof getStaticProps>) {
  // will resolve posts to type Post[] although it will be undefined if router.isFallback is true
}

export default Blog

Describe the solution you'd like

I'd like to have the possibility to pass the getStaticPaths as a second argument to the InferGetStaticPropsType type like this:

InferGetStaticPropsType<typeof getStaticProps, typeof getStaticPaths>

The InferGetStaticPropsType type would infer if the props can be an empty object based upon the value of fallback returned from getStaticPaths.

Describe alternatives you've considered

An alternative is to type the props manually if using fallback: true like this:

function Blog({ posts }: InferGetStaticPropsType<typeof getStaticProps> | {}) {
  // will resolve posts to type Post[] although it will be undefined if router.isFallback is true
}
@tomdohnal tomdohnal changed the title Correct type inference when using fallback: true Correct type inference when using fallback: true Nov 2, 2020
@timneutkens timneutkens added this to the 10.x.x milestone Nov 3, 2020
@Timer Timer removed this from the 10.x.x milestone Nov 3, 2020
@Timer Timer added this to the 10.x.x milestone Nov 18, 2020
@Timer Timer modified the milestones: 10.x.x, backlog Jan 6, 2021
@dsbrianwebster
Copy link

dsbrianwebster commented Mar 29, 2021

Adding on to this bug as I think the following is similar-enough to tackle in the same issue....

InferGetStaticPropsType will also fail when consider conditional returns from getStaticProps, like a notFound response.

props is never with a notFound condition is present

Screen Shot 2021-03-29 at 8 25 40 AM

works as expected with out a notFound condition

Screen Shot 2021-03-29 at 8 25 50 AM

@flybayer
Copy link
Contributor

Looks like this will hopefully be solved with Typescript 4.9!

https://twitter.com/leeerob/status/1563540593003106306

image

@eps1lon eps1lon assigned eps1lon and unassigned eps1lon Apr 12, 2024
@samcx samcx removed the kind: bug label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

7 participants