Typesafe markdown blog with Astro 2.0

Typesafe markdown blog with Astro 2.0

  • astro
  • typescript
  • content collection
published 2023-01-29

Astro just released their 2.0 version with lots of goodies, and the treat most important to this post is the introduction of content collections.

With the recent release of Astro 2.0 creating a type-safe blog has never been easier!

Content collections

With content collections you get to write typesafe markdown files by specifying a ZOD schema that decribes your posts, e.g:

const blog = defineCollection({
  schema: z.object({
    // Define your expected frontmatter properties
    title: z.string(),
    // Mark certain properties as optional
    draft: z.boolean().optional(),
    // Transform datestrings to full Date objects
    publishDate: z.string().transform((val) => new Date(val))
    // Improve SEO with descriptive warnings
	description: z.string()
    // ...


Saying that this is useful would be a gross understatement, head on over to the Astro docs to find out more about this awesome feature!