To type Astro props, declare an interface Props (or type Props) in the component’s frontmatter and read the values from Astro.props. Astro picks up that interface automatically, so your editor and astro check flag missing, misspelled or wrongly typed props wherever the component is used, with no generic or import needed.
This guide covers the patterns you’ll use on real components in Astro 7: defaults, optional and union props, extending HTML attributes, polymorphic as props, reusing another component’s props, and typing props that come from getStaticPaths().
Key Takeaways
interface Propsin the frontmatter is all Astro needs to type-checkAstro.props.- Set defaults with destructuring:
const { size = 'md' } = Astro.props;.- Extend
HTMLAttributes<'a'>fromastro/typesto accept every native attribute and spread the rest.- Rename
classwhile destructuring (class: className) becauseclassis a reserved word.- Use
InferGetStaticPropsType<typeof getStaticPaths>to type page props fromgetStaticPaths().
The basic Props interface
Here’s a card component with typed props:
---
// src/components/Card.astro
interface Props {
title: string;
href: string;
description?: string;
tags?: string[];
}
const { title, href, description, tags = [] } = Astro.props;
---
<article class="card">
<h2><a href={href}>{title}</a></h2>
{description && <p>{description}</p>}
{tags.length > 0 && (
<ul>{tags.map((tag) => <li>{tag}</li>)}</ul>
)}
</article>
Now any usage is checked:
---
// src/pages/index.astro
import Card from '../components/Card.astro';
---
<Card title="Astro tips" href="/tips/" /> <!-- OK -->
<Card title="Astro tips" /> <!-- Error: href is missing -->
<Card title="Astro tips" href="/tips/" tags="astro" /> <!-- Error: tags must be string[] -->
The errors show in VS Code with the official Astro extension, and in the terminal when you run npx astro check. astro build does not type-check, so add astro check to CI if you want type errors to fail a deploy.
type Props = { ... } works the same as interface Props. Use whichever your codebase prefers; interfaces are slightly easier to extend.
Default values and optional props
Mark a prop optional with ? and give it a default in the destructuring:
---
// src/components/Button.astro
interface Props {
variant?: 'primary' | 'secondary' | 'ghost';
size?: 'sm' | 'md' | 'lg';
disabled?: boolean;
}
const { variant = 'primary', size = 'md', disabled = false } = Astro.props;
---
<button class:list={['btn', `btn-${variant}`, `btn-${size}`]} disabled={disabled}>
<slot />
</button>
String literal unions like 'primary' | 'secondary' | 'ghost' give autocompletion when you use the component, and a typo such as variant="primay" becomes an error rather than a missing style.
Boolean props work like HTML attributes: <Button disabled /> passes true.
Extending native HTML attributes
A wrapper around <a> or <button> should accept every attribute the native element does (target, rel, aria-*, data-*) without you listing them. Import HTMLAttributes from astro/types and extend it:
---
// src/components/Link.astro
import type { HTMLAttributes } from 'astro/types';
interface Props extends HTMLAttributes<'a'> {
external?: boolean;
}
const { external = false, class: className, ...rest } = Astro.props;
---
<a
class:list={['link', className]}
{...(external ? { target: '_blank', rel: 'noopener noreferrer' } : {})}
{...rest}
>
<slot />
</a>
Three things are happening:
HTMLAttributes<'a'>includeshref,target,rel, ARIA attributes and the rest of the anchor’s attributes, with correct types.classis renamed toclassNameduring destructuring, becauseclassis a reserved word in JavaScript and can’t be a variable name....restcollects everything else and spreads it onto the element, so<Link href="/about/" aria-current="page">just works.
class:list merges your base classes with whatever the parent passes, and drops undefined values. The Astro directives cheatsheet covers class:list in more detail.
Polymorphic components with an as prop
Sometimes one component needs to render different elements: a heading that can be h1 to h6, or a button that becomes a link. Polymorphic from astro/types types the attributes based on the as value:
---
// src/components/Heading.astro
import type { HTMLTag, Polymorphic } from 'astro/types';
type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }>;
const { as: Tag = 'h2', ...rest } = Astro.props;
---
<Tag {...rest}><slot /></Tag>
---
// src/pages/index.astro
import Heading from '../components/Heading.astro';
---
<Heading as="h1" id="top">Welcome</Heading>
<Heading as="a" href="/blog/">Read the blog</Heading>
With as="a", href is accepted; with as="h1", an href would be an error. The capitalized Tag variable is required: Astro renders lowercase names as literal HTML tags and capitalized ones as dynamic elements or components.
Reusing another component’s props
When a component wraps another one, don’t copy its props by hand. ComponentProps reads them from the component itself:
---
// src/components/FeaturedCard.astro
import type { ComponentProps } from 'astro/types';
import Card from './Card.astro';
type Props = ComponentProps<typeof Card> & {
badge: string;
};
const { badge, ...cardProps } = Astro.props;
---
<div class="featured">
<span class="badge">{badge}</span>
<Card {...cardProps} />
</div>
If Card gains a new required prop later, FeaturedCard picks it up and its call sites are checked too.
Typing props from getStaticPaths
For dynamic routes, page props come from getStaticPaths(). Instead of writing the type twice, infer it:
---
// src/pages/blog/[id].astro
import type { InferGetStaticParamsType, InferGetStaticPropsType, GetStaticPaths } from 'astro';
import { getCollection, render } from 'astro:content';
export const getStaticPaths = (async () => {
const posts = await getCollection('blog');
return posts.map((post) => ({
params: { id: post.id },
props: { post },
}));
}) satisfies GetStaticPaths;
type Params = InferGetStaticParamsType<typeof getStaticPaths>;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { id } = Astro.params as Params;
const { post } = Astro.props;
const { Content } = await render(post);
---
<h1>{post.data.title}</h1>
<Content />
satisfies GetStaticPaths checks the function’s shape while keeping its exact return type, which is what the Infer* helpers read. post is now typed as a blog collection entry, so post.data.title autocompletes from your schema. The getStaticPaths guide covers pagination and nested params.
Typing content collection entries as props
Components that receive a collection entry should use CollectionEntry:
---
// src/components/PostCard.astro
import type { CollectionEntry } from 'astro:content';
interface Props {
post: CollectionEntry<'blog'>;
}
const { post } = Astro.props;
const { title, pubDate } = post.data;
---
<a href={`/blog/${post.id}/`}>
<h3>{title}</h3>
<time datetime={pubDate.toISOString()}>{pubDate.toLocaleDateString('en-US')}</time>
</a>
The type comes from the schema in src/content.config.ts, so renaming a frontmatter field there surfaces every component that still uses the old name. The TypeScript types cheatsheet lists the other built-in types.
Props on framework islands
Props for React, Vue or Svelte components are typed in those components, using that framework’s normal approach. Astro checks the props you pass against them. One extra rule applies to islands with a client:* directive: props are serialized into the HTML, so they must be data. Functions are dropped, and class instances lose their methods. Our article on Astro islands architecture lists what serializes.
Setting up TypeScript for strict props
New projects extend astro/tsconfigs/strict or strictest. Strict mode makes optional props T | undefined, which is what catches forgotten defaults:
{
"extends": "astro/tsconfigs/strict",
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
Run the checker:
# terminal
npx astro check
The first run offers to install @astrojs/check and typescript. Add it to your build script ("build": "astro check && astro build") to block deploys on type errors.
Common Astro props mistakes
- Destructuring
classdirectly.const { class } = Astro.propsis a syntax error. Rename it:const { class: className } = Astro.props. - Forgetting the default for an optional prop. In strict mode,
size?: 'sm' | 'md'isundefinedwhen omitted, so a template class built from it renders asbtn-undefined. Give it a default in the destructuring. - Writing page Props by hand. Pages receive props from
getStaticPaths(), and a hand-writteninterface Propsdrifts out of sync with what the function returns. UseInferGetStaticPropsTypeinstead. - Relying on
astro buildto catch type errors. It doesn’t. Only the editor andastro checkdo. - Typing
Astro.propswithas. Casting hides mistakes at the call site. DeclarePropsand let Astro infer it.
Komal themes use typed props throughout their components, which makes them easier to customize safely; you can browse Astro themes or read the folder structure docs to see where components live. The official Astro TypeScript guide covers the full set of utility types.
FAQ
How do I define props in an Astro component?
Declare interface Props with the prop names and types in the component’s frontmatter, then destructure them from Astro.props. Astro uses that interface automatically for type checking wherever the component is used.
How do I set default props in Astro?
Use JavaScript destructuring defaults: const { size = 'md', tags = [] } = Astro.props;. Mark those props optional with ? in the Props interface so callers can leave them out.
How do I pass the class attribute to an Astro component?
Accept it as class in your props, rename it while destructuring with const { class: className } = Astro.props;, and apply it with class:list={['base', className]}. Extending HTMLAttributes includes class automatically.
Does Astro check prop types at build time?
No. astro build doesn’t run the type checker. Run npx astro check locally or in CI, or prepend it to your build script, to fail on type errors.
Can I use type instead of interface for Astro props?
Yes. type Props = { ... } and interface Props { ... } both work. Use type when you need unions or intersections, such as ComponentProps<typeof Card> & { badge: string }.