🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Astro Fundamentals••By Komal Nakrani

Astro TypeScript Config: tsconfig Presets Explained

Astro TypeScript setup explained: the base, strict and strictest tsconfig presets, what each option does, typing props, path aliases, astro check and astro sync.

#Astro#TypeScript#tsconfig#Developer Experience

Astro TypeScript support is built in: every project has a tsconfig.json that extends one of three presets shipped with Astro (astro/tsconfigs/base, strict or strictest), and .astro, .ts and .tsx files are type-checked by your editor and by astro check. Astro itself doesn’t type-check during astro build; it strips types and builds. The preset you choose only controls how strict the checks are in the editor and in astro check.

This explainer goes through each preset’s actual options, how Astro generates types, how to type component props, set up path aliases, and add type checking to CI.

Key Takeaways

  • New projects extend astro/tsconfigs/strict, which is base plus TypeScript’s strict: true.
  • strictest adds options like noUncheckedIndexedAccess and exactOptionalPropertyTypes.
  • .astro/types.d.ts (generated by astro sync) provides types for content collections, astro:env and other virtual modules.
  • astro build doesn’t fail on type errors; run astro check in CI to catch them.
  • Type component props with an interface Props in the frontmatter.

The default Astro tsconfig.json

A new Astro 7 project has this tsconfig.json:

{
  "extends": "astro/tsconfigs/strict",
  "include": [".astro/types.d.ts", "**/*"],
  "exclude": ["dist"]
}

extends pulls in one of Astro’s presets from node_modules/astro/tsconfigs/. include lists the generated types file plus every file in the project, and exclude keeps the build output out of checks. You add your own compilerOptions on top, and they override the preset.

What the base preset sets

base is the foundation of the other two. Every option exists because of how Astro and Vite build your code:

Option Value Why
target, module ESNext Allows top-level await and modern syntax in frontmatter and scripts
moduleResolution Bundler Matches how Vite resolves imports, including package exports
allowImportingTsExtensions true Lets you import ./utils.ts with its extension
resolveJsonModule true Allows import data from './data.json'
verbatimModuleSyntax true Requires import type for type-only imports, so types never end up in bundles
isolatedModules true Each file must compile on its own, as Vite’s transpiler expects
noEmit true TypeScript never writes files; Vite handles the build
jsx preserve Leaves JSX for the framework integration (React, Preact, Solid) to transform
allowJs true Mixes .js and .ts files
esModuleInterop true Imports CommonJS packages cleanly
skipLibCheck true Skips type-checking .d.ts files in dependencies, for speed
forceConsistentCasingInFileNames true Catches ./Card vs ./card imports that break on Linux

base doesn’t turn on strict, so implicit any and nullable values aren’t flagged. It’s for projects that use TypeScript lightly or are migrating from JavaScript.

The option most people notice is verbatimModuleSyntax. Importing a type without type is an error:

// src/lib/posts.ts
import type { CollectionEntry } from 'astro:content';
import { getCollection } from 'astro:content';

export async function latest(): Promise<CollectionEntry<'blog'>[]> {
  const posts = await getCollection('blog');
  return posts.sort((a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()).slice(0, 5);
}

The strict preset

strict extends base and adds one option: "strict": true. That single flag enables TypeScript’s strict family, including strictNullChecks, noImplicitAny, strictFunctionTypes, strictBindCallApply, strictPropertyInitialization, alwaysStrict and useUnknownInCatchVariables.

In practice, strict mode catches the bugs that matter most in Astro code: an optional prop used without a default, an Astro.params value assumed to exist, or a find() result used without checking it. It’s the default for new projects and the right choice for most sites.

The strictest preset

strictest extends strict and adds stricter checks:

Option Effect
noUncheckedIndexedAccess arr[0] and record[key] are typed as possibly undefined
exactOptionalPropertyTypes prop?: string can be omitted but not set to undefined explicitly
noImplicitReturns Every code path in a function must return
noFallthroughCasesInSwitch case blocks can’t fall through silently
noImplicitOverride Overriding class methods requires override
noUnusedLocals, noUnusedParameters Unused variables and parameters are errors
allowUnreachableCode: false, allowUnusedLabels: false Dead code and unused labels are errors instead of warnings

noUncheckedIndexedAccess is the most useful and the most noisy. It forces checks like this:

---
// src/pages/blog/index.astro
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');
const featured = posts[0];
---
{featured && <h2>{featured.data.title}</h2>}

Choose strictest for libraries, shared component packages or teams that want maximum safety. For a content site, strict plus a few chosen options is often a better balance.

Choosing and customizing an Astro TypeScript preset

Preset Use when
base Migrating a JavaScript project, or prototyping
strict Most Astro sites and themes (default)
strictest Libraries, design systems, teams that want every check

You can pick a preset and adjust individual options:

{
  "extends": "astro/tsconfigs/strict",
  "include": [".astro/types.d.ts", "**/*"],
  "exclude": ["dist"],
  "compilerOptions": {
    "noUncheckedIndexedAccess": true,
    "paths": {
      "@components/*": ["./src/components/*"],
      "@layouts/*": ["./src/layouts/*"],
      "@lib/*": ["./src/lib/*"]
    }
  }
}

Path aliases

The paths option above replaces long relative imports. Astro reads paths from tsconfig.json and configures Vite to resolve the aliases, so they work in the build as well as in the editor:

---
// src/pages/about.astro
import BaseLayout from '@layouts/BaseLayout.astro';
import Card from '@components/Card.astro';
---
<BaseLayout title="About">
  <Card title="Our story" />
</BaseLayout>

Restart the dev server after changing paths.

Generated types: .astro/types.d.ts and astro sync

Astro’s virtual modules (astro:content, astro:env/client, astro:env/server, astro:actions and others) have types generated from your project. astro sync writes them to the .astro/ folder, and include pulls them in through .astro/types.d.ts.

You rarely run it by hand, because astro dev, astro build and astro check all sync first. Run npx astro sync when your editor shows getCollection('blog') entries typed as any after a schema change, or right after cloning a project. Add .astro/ to .gitignore; it’s regenerated on demand.

Content collection schemas are the biggest payoff: once synced, post.data.pubDate is a Date and post.data.tags is a string[], straight from your Zod schema. The content collections guide shows how schemas become types.

Typing component props

In .astro files, declare an interface Props (or type Props) in the frontmatter. Astro uses it to type Astro.props and to check every place the component is used:

---
// src/components/Button.astro
import type { HTMLAttributes } from 'astro/types';

interface Props extends HTMLAttributes<'a'> {
  variant?: 'primary' | 'secondary';
}

const { variant = 'primary', class: className, ...attrs } = Astro.props;
---
<a class:list={['btn', `btn--${variant}`, className]} {...attrs}>
  <slot />
</a>

astro/types also exports Polymorphic for components that render different tags through an as prop, and ComponentProps to extract another component’s props type. The props and TypeScript guide covers these patterns, and the TypeScript types cheatsheet lists every type Astro exports.

Type checking with astro check

Editor squiggles aren’t a guarantee, and astro build ignores type errors. To fail a CI build on type errors, run astro check:

npx astro check

The first run asks to install @astrojs/check and typescript as dev dependencies. It checks .astro files as well as .ts, which the plain tsc command can’t do. Add it to your build script so every deploy is checked:

{
  "scripts": {
    "dev": "astro dev",
    "check": "astro check",
    "build": "astro check && astro build"
  }
}

For editor support, install the official Astro extension for VS Code (or the Astro language server for other editors), which uses the same checks.

Komal themes are TypeScript Astro 7 projects, so they ship with a preset-based tsconfig.json, typed props and generated content types already in place; you can see the setup in any Astro theme after cloning. For theme folder layout, the folder structure docs explain where each file lives. Astro’s official TypeScript guide is the full reference.

FAQ

Does Astro support TypeScript out of the box?

Yes. Astro compiles TypeScript in .astro, .ts and .tsx files without extra setup, and every new project includes a tsconfig.json extending an Astro preset. Type checking runs in your editor and with astro check.

What is the difference between Astro’s strict and strictest presets?

strict is the base preset plus TypeScript’s strict: true. strictest adds further checks such as noUncheckedIndexedAccess, exactOptionalPropertyTypes, noImplicitReturns and errors for unused variables and parameters.

Does astro build fail on TypeScript errors?

No. astro build transpiles TypeScript without checking types, so builds succeed even with type errors. Run astro check before the build (for example astro check && astro build) to fail on type errors.

Why are my content collection types showing as any?

The generated types are out of date or not included. Run npx astro sync, make sure tsconfig.json includes .astro/types.d.ts, and restart your editor’s TypeScript server.

Can I use JavaScript instead of TypeScript in Astro?

Yes. allowJs is on in every preset, so .js files and untyped frontmatter work. You can still keep a tsconfig.json with the base preset for editor features like autocomplete.