sync() function generates TypeScript types for all Astro modules in your project. This sets up a src/env.d.ts file for type inferencing and defines the astro:content module for the Content Collections API.
Import
Signature
Parameters
AstroInlineConfig
required
Configuration object for your Astro project. Supports all options from
astro.config.mjs.string
The root directory of your Astro project. Defaults to the current working directory.
'debug' | 'info' | 'warn' | 'error' | 'silent'
default:"'info'"
Controls the verbosity of logging output.
boolean
default:"false"
Force a clean sync by clearing the content layer cache before generating types.
Return Value
Returns aPromise<void> that resolves when type generation completes successfully.
What It Does
Thesync() function performs the following tasks:
- Generates
src/env.d.ts: Creates TypeScript definitions for Astro’s built-in modules - Creates
.astro/types.d.ts: Generates type definitions in your cache directory - Generates Content Collections types: Creates the
astro:contentmodule with types for your content collections - Syncs content layer: Updates the content layer cache with the latest content
- Generates environment types: Creates types for environment variables defined in your schema
Examples
Basic Sync
Force Clean Sync
Clear the cache and regenerate all types:Silent Mode
Pre-Build Hook
Run sync before building to ensure types are up-to-date:Watch Script
Create a custom watch script that regenerates types on changes:CI/CD Type Check
Content Collections Sync
Ensure content collection types are generated before running tests:Multiple Projects
Error Handling
Development Workflow
Monorepo Setup
When to Use
You should runsync() when:
- Adding or modifying content collections
- Adding or changing content schemas
- Setting up a new project
- After pulling changes that affect content
- Before running type checks in CI/CD
- When content collection types are out of date