Skip to main content
The 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.
The JavaScript API is experimental and may change in future releases.

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 a Promise<void> that resolves when type generation completes successfully.

What It Does

The sync() function performs the following tasks:
  1. Generates src/env.d.ts: Creates TypeScript definitions for Astro’s built-in modules
  2. Creates .astro/types.d.ts: Generates type definitions in your cache directory
  3. Generates Content Collections types: Creates the astro:content module with types for your content collections
  4. Syncs content layer: Updates the content layer cache with the latest content
  5. 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 run sync() 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