Skip to content
34 changes: 23 additions & 11 deletions src/content/docs/en/basics/layouts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,34 @@ const { title, fancyJsHelper } = Astro.props;

<ReadMore>Learn more about Astro’s Markdown and MDX support in our [Markdown/MDX guide](/en/guides/markdown-content/).</ReadMore>

## Using one Layout for `.md`, `.mdx`, and `.astro`
### Using TypeScript with layouts

A single Astro layout can be written to receive the `frontmatter` object from `.md` and `.mdx` files, as well as any named props passed from `.astro` files.
Any astro layout can be modified to introduce typesafety & autocompletion by providing the types for your props:

In the example below, the layout will display the page title either from a frontmatter YAML `title` property or from an Astro component passing a `title` attribute:

```astro /{?title}?/ /Astro.props[.a-z]*/
```astro ins={2-7} title="src/components/MyLayout.astro"
---
// src/components/MyLayout.astro
const { title } = Astro.props.frontmatter || Astro.props;
interface Props {
title: string;
description: string;
publishDate: string;
viewCount: number;
}
const { title, description, publishDate, viewCount } = Astro.props;
---
<html>
<head></head>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content={description}>
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
<slot />
<header>
<p>Published on {publishDate}</p>
<p>Viewed by {viewCount} folks</p>
</header>
<main>
<slot />
</main>
</body>
</html>
```
Expand Down