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

page prop is type never with InferGetStaticPropsType as it has no signature matching the paginate function #6507

Closed
1 task done
david-abell opened this issue Mar 10, 2023 · 2 comments
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@david-abell
Copy link

What version of astro are you using?

2.1.2

Are you using an SSR adapter? If so, which one?

none

What package manager are you using?

npm

What operating system are you using?

windows

What browser are you using?

chrome

Describe the Bug

InferGetStaticPropsType does not work with getStaticPaths({ paginate } {...}

The new helper type type Props = InferGetStaticPropsType<typeof getStaticPaths> does not work when used with getStaticPaths() and the paginate option such as the basic docs pagination example found here.

See below codesandbox run script check

The signature infers types from a function with no arguments T extends () =>...

export type InferGetStaticPropsType<T> = T extends () => Promise<infer R> ? R extends Array<infer U> ? U extends {
    props: infer P;

I tried modifying the signature to accept parameters:

 export type InferGetStaticPropsType<T> = T extends (...args: any[]) => Promise<infer R> ...

I was specifically trying to use this with collections:

export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
  const collection = (await getCollection('projects')).sort((a, b) => a.data.pubDate.valueOf() - b.data.pubDate.valueOf());
  return paginate(collection, { pageSize: 6 });
};

The paginate function appears not to return the collection type of CollectionEntry<"projects">[] but instead returns only type GetStaticPathsResult. It would be nice if this were a generic so that inferGetStaticPropsType could infer the collectionEntry as Props.

The old way still of course works as expected:

interface Props {
  page: Page<CollectionEntry<'projects'>>;
}

const { page } = Astro.props;

Link to Minimal Reproducible Example

https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/blog?on=codesandbox&file=%2FREADME.md

Participation

  • I am willing to submit a pull request for this issue.
@Princesseuh Princesseuh self-assigned this Mar 23, 2023
@Princesseuh Princesseuh added the - P3: minor bug An edge case that only affects very specific usage (priority) label Mar 23, 2023
@Princesseuh
Copy link
Member

Princesseuh commented Mar 30, 2023

The problem isn't that it doesn't work with paginate, it's that it doesn't work when getStaticPaths is typed because then it cannot then infer the return value.

What you should do instead, is use TS 4.7's satisfies:

export const getStaticPaths = (async ({ paginate }) => {
  ...
}) satisfies GetStaticPaths;

@Princesseuh
Copy link
Member

Princesseuh commented Mar 30, 2023

Since this is not possible to fix, I instead updated the documentation to point towards satisfies: withastro/docs#2963

Additionally, the investigation here led to find an unrelated issue with it, fixed it here: #6711 (this PR also updates the comments to show the proper way to do it)

Thank you for reporting this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

2 participants