Skip to main content
Astro provides first-class TypeScript support out of the box. This guide covers how to configure TypeScript in your Astro project for optimal type safety and developer experience.

Quick Start

Astro includes TypeScript support by default. No installation required - just start using .ts and .astro files.
1

Generate types

Run astro sync to generate TypeScript types for your content collections and integrations:
2

Create tsconfig.json

Add a tsconfig.json file to your project root:
tsconfig.json
3

Start developing

Use TypeScript in your .astro, .ts, and .tsx files:
src/pages/index.astro

TypeScript Configuration

Using Astro’s Presets

Astro provides three TypeScript presets to choose from:

Custom TypeScript Configuration

You can extend Astro’s presets with your own settings:
tsconfig.json
  • baseUrl: Base directory for resolving non-relative module names
  • paths: Path mapping for module resolution (like aliases)
  • target: ECMAScript target version (default: ES2022)
  • module: Module code generation (default: ESNext)
  • lib: Library files to include (default: ["ES2022"])
  • jsx: JSX code generation (Astro handles this automatically)
  • resolveJsonModule: Allow importing .json files
  • allowJs: Allow JavaScript files in your project

Include and Exclude Patterns

Control which files TypeScript processes:
tsconfig.json
Always include .astro/types.d.ts - this file contains generated types for content collections and integrations.

Type Checking

Astro doesn’t type-check during development for performance. Use these methods to check types:

During Development

Install the Astro VSCode extension for inline type checking.Configure your editor to show TypeScript errors:
.vscode/settings.json

In CI/CD

Add type checking to your build process:
package.json

Generated Types

Astro automatically generates types for your project:

Content Collections

Types are generated for content collections in .astro/types.d.ts:
src/content/config.ts
Use generated types in your code:
src/pages/blog/[...slug].astro

Integration Types

Integrations can add their own type definitions. Run astro sync to generate them:
Add astro sync to your prepare script to run it automatically on npm install:
package.json

Type Safety in Astro Files

Component Props

Define types for component props:
src/components/Card.astro

Frontmatter Type Assertions

Use type assertions for complex data:
src/pages/about.astro

Global Types

Define global types in a .d.ts file:
src/types/global.d.ts

Import Aliases

Configure path aliases for cleaner imports:
1

Configure tsconfig.json

tsconfig.json
2

Use aliases in your code

src/pages/index.astro
Astro provides a default @/* alias that maps to src/*. You can use this without additional configuration.

Framework-Specific TypeScript

React

src/components/Counter.tsx

Vue

src/components/Counter.vue

Svelte

src/components/Counter.svelte

Troubleshooting

Run astro sync to regenerate types:
If issues persist, delete .astro/types.d.ts and run sync again.
Ensure .astro/types.d.ts is included in your tsconfig.json:
tsconfig.json
Restart your TypeScript server in your editor.
Verify your tsconfig.json has both baseUrl and paths configured:
tsconfig.json
If you’re getting too many errors with strict mode, start with the base preset:
tsconfig.json
Gradually enable strict options as you fix issues.

Best Practices

Use strict mode

Enable strict TypeScript checks to catch errors early

Run astro sync

Keep generated types up-to-date, especially after schema changes

Type component props

Always define interfaces for component props

Check in CI/CD

Add astro check to your CI pipeline

Next Steps

Content Collections

Learn about type-safe content collections

Astro Config

Configure your Astro project

Environment Variables

Type-safe environment variables

Editor Setup

Set up your editor for Astro