#getCollection has any type

9 messages · Page 1 of 1 (latest)

wind rune
#

When I define a collection with defineCollection() and Zod, the object I obtaiun with getCollection has any type

sturdy terraceBOT
#

Hi @wind rune. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

When you define a collection with defineCollection() and provide a Zod schema, Astro should automatically infer and apply TypeScript types to the objects returned by getCollection(). This means that, if everything is set up correctly, the entries you get from getCollection() should not have the any type—they should be strongly typed according to your schema.

Here’s how it should work:

import { defineCollection, z, getCollection } from 'astro:content';

const blog = defineCollection({
  loader: /* ... */,
  schema: z.object({
    title: z.string(),
    description: z.string(),
    pubDate: z.date(),
  }),
});

export const collections = { blog };

// Usage in a page or component
const posts = await getCollection('blog');
// posts should be typed according to your schema

If you are still seeing any types, possible reasons include:

  • Your defineCollection() is missing a schema, or the schema is not correctly defined.
  • Your TypeScript setup is not picking up Astro’s types, or you need to restart your dev server after making changes to src/content.config.ts.
  • You are using an outdated or legacy API, or your collection is not properly registered in the collections export.

Astro’s documentation states:

Schemas also power Astro’s automatic TypeScript typings for your content. 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.
Defining the collection schema

#

If you have followed these steps and still get any, double-check your schema and ensure you have restarted the dev server. If the problem persists, there may be an issue with your project setup or a bug.

If you are using legacy collections or have not updated to the new Content Layer API, you may not get full type inference. The new API is required for automatic TypeScript types to work as described above Defining the collection schema.

If you need more help, please provide your src/content.config.ts and how you are calling getCollection().

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@wind rune Kapa.ai is still learning and improving, please let me know how I did by reacting below.

wind rune
#

@sturdy terrace
this is my code:

import { defineCollection, z } from "astro:content";
// z -> zod schema

const books = defineCollection({
  schema: z.object({
    title: z.string(),
    author: z.string(),
    img: z.string(),
    readtime: z.number(),
    description: z.string(),
    buy: z.object({
      spain: z.string().url(),
      usa: z.string().url(),
    }),
  })
})

export const collections = { books }
sturdy terraceBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Your defineCollection usage and Zod schema are correct. With this setup, Astro should automatically generate TypeScript types for your books collection, and getCollection('books') should return entries typed according to your schema.

If you are still seeing any types when using getCollection('books'), here are the most likely causes and solutions based on the official documentation:

  1. Type Generation Requires astro sync
    Astro generates types for content collections automatically when you run astro dev, astro build, or astro check. If you are not seeing types, try restarting your dev server or manually running astro sync to regenerate types CLI Reference: astro sync.

  2. Check Your TypeScript Configuration
    Make sure your tsconfig.json is set up correctly. You should have "strictNullChecks": true and "allowJs": true in your compilerOptions if you are not extending Astro’s strict presets Content collections guide.

  3. Correct Usage of getCollection
    When you use getCollection('books'), the returned entries should be typed as CollectionEntry<'books'>. You can import this type for explicit typing if needed:

    import type { CollectionEntry } from 'astro:content';
    const books = await getCollection('books');
    // books: CollectionEntry<'books'>[]
    

    Content Collections API Reference

#
  1. Schema Must Be Present
    You already have a schema, which is required for type inference. If the schema is missing or not exported, types will default to any Defining the collection schema.

If you have followed all these steps and still get any, try:

  • Restarting your editor (sometimes VS Code needs a restart to pick up new types).
  • Ensuring there are no errors in your src/content.config.ts.

If the issue persists, there may be a problem with your project setup or a bug in Astro.

Summary:
Your code is correct. If you are still getting any types, ensure you have run or restarted astro dev, check your TypeScript config, and make sure your editor is picking up the generated types. For more details, see the Defining the collection schema and astro sync documentation.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: