Quick Start
Astro includes TypeScript support by default. No installation required - just start using.ts and .astro files.
Generate types
Run
astro sync to generate TypeScript types for your content collections and integrations:TypeScript Configuration
Using Astro’s Presets
Astro provides three TypeScript presets to choose from:- strict (recommended)
- base
- strictest
The strictest TypeScript settings. Recommended for most projects.Enables:
tsconfig.json
strict: truestrictNullChecks: truenoUnusedLocals: truenoUnusedParameters: true- And more strict checks
Custom TypeScript Configuration
You can extend Astro’s presets with your own settings:tsconfig.json
Common compilerOptions
Common compilerOptions
baseUrl: Base directory for resolving non-relative module namespaths: 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.jsonfilesallowJs: 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
- VSCode
- CLI
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
src/pages/blog/[...slug].astro
Integration Types
Integrations can add their own type definitions. Runastro sync to generate them:
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: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
Types not updating
Types not updating
Run If issues persist, delete
astro sync to regenerate types:.astro/types.d.ts and run sync again.Cannot find module errors
Cannot find module errors
Ensure Restart your TypeScript server in your editor.
.astro/types.d.ts is included in your tsconfig.json:tsconfig.json
Path aliases not working
Path aliases not working
Verify your
tsconfig.json has both baseUrl and paths configured:tsconfig.json
Strict mode errors
Strict mode errors
If you’re getting too many errors with strict mode, start with the Gradually enable strict options as you fix issues.
base preset:tsconfig.json
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 pipelineNext 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