#Get collection type
1 messages · Page 1 of 1 (latest)
Hi @languid helm. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To get the type of a collection in Astro, you can use the CollectionEntry type utility from astro:content. This allows you to properly type your collection entries when working with TypeScript.
Here's how to import and use the CollectionEntry type:
import type { CollectionEntry } from 'astro:content';
// Use it with the name of your collection
type BlogPost = CollectionEntry<'blog'>;
// You can now use this type for components or functions
interface Props {
post: BlogPost;
}
The CollectionEntry is a generic type that you can use with the name of the collection you're working with. For example, if you have a "blog" collection, you would use CollectionEntry<'blog'> to get its type.
If you need a union of all collection names defined in your content config file, you can use the CollectionKey type:
import { type CollectionKey } from 'astro:content';
// This will be a union of all your collection names
// e.g. 'blog' | 'authors' | etc.
This is particularly useful when creating generic functions that work with multiple collections.
Content Collections API Reference provides more details about these and other type utilities available in Astro's content collections system.