Hey y'all I am a new developer trying to wrap my head around using typescript for the first time. Currently I am running into an issue of losing type definition when defining the render template of getStaticPaths.
So I have gotten typescript to work up until the following lines, where it believes both tag and posts are type never.
const { posts } = Astro.props;```
I would also appreciate a more simplified description of what those lines are doing as I truly do not understand what is happening.
Below is my code for this page following the astro tutorial guide:
import BaseLayout from "...";
import Formatter from "..."
import { getCollection } from "astro:content";
import type { CollectionEntry } from "astro:content";
export async function getStaticPaths(): Promise<Object> {
const allPosts:CollectionEntry<"blog">[] = await getCollection("blog");
const uniqueTags: string[] = [
...new Set(allPosts.map((post) => post.data.tags).flat()),
];
return uniqueTags.map((tag) => {
const filteredPosts = allPosts.filter((post) =>
post.data.tags.includes(tag),
);
//the params tag here is the name of each dynamic route
//the props variable is the data to pass to each page
return {
params: { tag },
props: { posts: filteredPosts },
};
});
}
// What is going on here??
// How can I type both variables
const { tag } = Astro.params;
const { posts } = Astro.props;
// The following is the layout of what will apear on the dynamicly made static paths
<BaseLayout title=My ${tag} Blog Posts , heading=Only ${tag}>
<p class="text-lg font-medium">Posts tagged with {tag}</p>
<ul>
{
posts.map((post) => (
<Formatter
url={/blog/${post.slug}/}
draft={post.data.draft}
title={post.data.title}
pubDate={post.data.pubDate}
tags={post.data.tags}
/>
))
}
</ul>
</BaseLayout>