Content Collections Types

Astro’s content collections are a great way to manage your markdown content, and while they provide great type-safety when using their API, they don’t expose the types of the content in a way that’s easy to use in your own functions. There are a few tricks we can use to do that though.

Collection List

A type that represents a union of all the collections in your project. We infer this from the getEntryBySlug function.

import { getEntryBySlug } from 'astro:content'
export type Collections = Parameters<typeof getEntryBySlug>[0]

Collection Specific Slugs

A type that represents the union of slugs for a given collection. We infer this from the CollectionEntry type. This isn’t perfect, because you need to specify the collection name, but should be pretty useful.

import type { CollectionEntry } from 'astro:content'
export type BlogSlug = CollectionEntry<'blog'>['slug']

All Slugs

A type that represents the union of slugs for all collections. We infer this from the getEntryBySlug function.

import { getEntryBySlug } from 'astro:content'
export type Slug = Parameters<typeof getEntryBySlug>[1]