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

fix getStaticPaths typing #8048

Closed

Conversation

hori-ryota
Copy link

Changes

fix typing around getStaticPaths

  • Be short and concise. Bullet points can help!

Testing

import type { InferGetStaticPropsType, PaginateFunction } from "astro";

type Post = {
  title: string;
  body: string;
};

export function getStaticPaths({
  paginate,
}: {
  paginate: PaginateFunction<Post>;
}) {
  return paginate(
    [
      {
        title: "title1",
        body: "body1",
      },
    ],
    { pageSize: 10 },
  );
}
type Props = InferGetStaticPropsType<typeof getStaticPaths>;

before

type Props = never

after

type Props = Props & {
    page: Page<Post>;
}

Docs

/cc @withastro/maintainers-docs for feedback!

@hori-ryota hori-ryota requested a review from a team as a code owner August 13, 2023 07:16
@changeset-bot
Copy link

changeset-bot bot commented Aug 13, 2023

🦋 Changeset detected

Latest commit: 5326190

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Aug 13, 2023
@Princesseuh
Copy link
Member

Princesseuh commented Aug 18, 2023

I overall like this, though a solution directly in our language tooling would still be preferable. It'll need some docs on our TypeScript page to explain how to type Paginate's type manually. It's also a bit unfortunate that it doesn't work with the satisfies GetStaticPaths pattern we show on our docs.

Additionally, a small bug I noticed:

export const getStaticPaths = (async ({ paginate }) => {
  const posts = await getCollection("blog");
  const paginated = posts.map((post) => {
    return {
      params: { slug: post.slug },
      props: { draft: false, title: post.data.title },
    };
  });

  return paginate(paginated, { pageSize: 10 });
}) satisfies GetStaticPaths;

In this code, paginate has the type PaginateFunction<GetStaticPathsItem, GetStaticPathsItem>, which cause params to be necessary in the second argument of paginate:
image

@Princesseuh
Copy link
Member

Superseded by #8229, thank you very much for your contribution!

@hori-ryota hori-ryota deleted the fix-getStaticPaths-typing branch September 1, 2023 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Page prop is type never with InferGetStaticPropsType
3 participants