#what is the type of frontmatter?
2 messages · Page 1 of 1 (latest)
The type of frontmatter or entry.data in your code is determined by the schema you've defined for your content collection. In Astro, you can define a schema for your collection which enforces consistent frontmatter or entry data within a collection. This schema guarantees that this data exists in a predictable form when you need to reference or query it. If any file violates its collection schema, Astro will provide a helpful error to let you know.
When you define a schema for your collection, Astro will automatically generate and apply a TypeScript interface to it. The result is full TypeScript support when you query your collection, including property autocompletion and type-checking.
Here's an example of how you might define a schema for a blog collection:
// 1. Import utilities from `astro:content`
import { z, defineCollection } from 'astro:content';
// 2. Define a `type` and `schema` for each collection
const blogCollection = defineCollection({
type: 'content', // v2.5.0 and later
schema: z.object({
title: z.string(),
tags: z.array(z.string()),
image: z.string().optional(),
}),
});
// 3. Export a single `collections` object to register your collection(s)
export const collections = {
'blog': blogCollection,
};
In this example, the frontmatter or entry.data would be of type { title: string, tags: string[], image?: string }.