#How to type a paginated content collection?

2 messages · Page 1 of 1 (latest)

tardy dew
#

Was able to find a few examples, notably from this thread:

Still failing to find a solution for actually having a correctly typed getStaticPaths usage, not sure if I'm setting this up correctly:

// pages/blog/[...page].astro
import type { GetStaticPaths, GetStaticPathsResult } from "astro";
import { getCollection } from "astro:content";

export const getStaticPaths: GetStaticPaths = async ({ paginate }) => {
  const posts = (await getCollection("blog")).sort(
    (a, b) => b.data.publishDate.valueOf() - a.data.publishDate.valueOf()
  );
  return paginate(posts, { pageSize: 5 });
};

const { page } = Astro.props;

^ In this version, page is typed as 'any'. Am I suppose to define the props as something specific? I can see that there are Page types in the astro.d.ts file but I'm not sure how they're intended to be used.

Edit: looks like I can add props to make this work. This seems to be working decently.

type Props = { page: Page<CollectionEntry<"blog">> };
tardy dew
#

Hmm, looks like this fails to parse if I have the return type : Promise<GetStaticPathsResult> on there, with Unexpected ":". Will remove from the original post