Components and layouts
Type component props
Declare the component contract with a Props interface and provide defaults for optional values.
8 minute lesson
Astro recognizes an interface named Props:
---
interface Props {
title: string
featured?: boolean
}
const { title, featured = false } = Astro.props
---
Now <Card featured="yes" /> is a type error, and omitting title is a type error. The interface documents the component at the point where people use it.
Optional properties use ?, and their defaults belong in the destructuring statement. Keep the type and the default consistent:
---
interface Props {
title: string
count?: number
}
const { title, count = 0 } = Astro.props
---
This checking happens in the editor and during type checking or build. It is not runtime validation. If props originate in a URL, form submission, CMS, or external API, validate that input before passing it to the component.
Think of Props as an internal programming contract, not a security boundary. It prevents accidental misuse by your code; it cannot make untrusted data truthful.
Lesson completed