add information about using TypeScript with layouts#8763
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
✅ Deploy Preview for astro-docs-2 ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
sarah11918
left a comment
There was a problem hiding this comment.
Thank you so much for running with this @at-the-vr ! I love clearing issues, and I think this is really helpful! Take a look at my comments and see what you think.
P.S. idea for another PR: the basic example, maybe we could also find a spot for something just like it on the astro components page so that where we talk about props, we show a TypeScript example there, too!
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
| </html> | ||
| ``` | ||
|
|
||
| You can override Astro's default `Props` to handle situations where your layout recieves props of different types: |
There was a problem hiding this comment.
Update: friction resolved by avoiding it in the first stage 😆
You can override ... different types is incorrect afaik because Props can be defined/override to include different types as described in snippet above and I can't think of what it should be 😓
Can any experienced around TypeScript ecosystem suggest a better approach? Thanks in advance ❤️
| </html> | ||
| ``` | ||
|
|
||
| You can override Astro's default `Props` to handle situations where your layout recieves props of different types: |
There was a problem hiding this comment.
| You can override Astro's default `Props` to handle situations where your layout recieves props of different types: | |
| You can also define custom `Props` to handle situations where your layout receives props of different types: |
@at-the-vr , Is it just the wording that is technically not correct to say? what do you think if we just go a little more general like this? 😅
Or, is the entire example not correct?
There was a problem hiding this comment.
Apologies for being blunt but the whole sentence You can override Astro's ... props of different types: happens to be incorrect with what I know of Types and Astro handling them. The suggestion is not doing any good here honestly.
Props can be custom in Astro and that's correct, but Astro.props.frontmatter is very specific. I believe its mentioned in Markdown Layout Props - Please correct me if I happen to be faulty here 😓
So when someone attempts the following (similar to OP's Issue linked)
---
// snippet from Blog Template
type Props = CollectionEntry<"blog">["data"];
const { title, description, pubDate, updatedDate, heroImage } = Astro.props.frontmatter || Astro.props;
---They receive the Property frontmatter does not exists Error.
With respect to the Blog template this issue can be resolved by CustomProps like this:
+ type CustomProps = CollectionEntry<"blog">["data"] & {
title: string,
description: string,
pubDate: Date,
updatedDate: Date,
heroImage: string,
}
+ const { title, description, pubDate, updatedDate, heroImage } :CustomProps =
Astro.props.frontmatter || Astro.props;
---and that's what I want to address in that sentence
My direction: Here's a way you can avoid Property frontmatter does not exists error
Docs direction: Have to rely on Sarah and Team Astro for this one 🙏
|
As I commented in #8934, an overlooked aspect of this is that content entry data is processed through Zod, which can transform frontmatter entries. For example, in the blog example, the I believe all frontmatter entries for a page would remain as they are parsed, as Strings or whatever basic types. My experience recently trying to use the BlogEntry.astro layout for a Markdown page was that Arbitrary transformations are possible through the Zod API, so it's not enough to rely on the frontmatter parser assigning types automatically, in the general case. I'm not well-versed in TypeScript but this is the hack way I found to do it (which probably should be combined with @at-the-vr's use of import { collections } from '../content/config';
const { author } = Astro.props.frontmatter ? collections.blog.schema.parse(Astro.props.frontmatter) : Astro.props;It might be worth rethinking and unifying frontmatter handling between pages and content entries, which would be more conceptually ideal, rather than requiring the user either use (and know to use) hacks like applying the schema parser in their layout, or duplicating type definitions. |
Is the " I am afraid to tell ya, a hack is not what Docs is supposed to tell its users. Additionally I could not make it work on my end 😞 without TypeScript complaining. Trying hard not to put down your suggestion but because its working I will ask if you can document this and share for future usecases. As for unifying frontmatter handling, perhaps someone with more experience in this area could provide insights or suggestions on how to approach this 🙌 |
- taking suggestions from Sarah Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
taking suggestions from Ryan
|
Thanks for updating this, @at-the-vr ! I do think we should maybe also remove the "two layouts in one" section here. I'm not sure it's really helpful anymore. Would you be willing to remove that entire section, too? Then, when you're happy with this, I think it's good to merge! |
sarah11918
left a comment
There was a problem hiding this comment.
Approving the new section as-is, and would also approve if you decided to delete the problematic existing section, too! 😄
I agree, but I think that the existing suggestion of |
This is a can of worms, and clearly I lack solution for this. There is one way how Official Portfolio does it which I find fitting your usecase. If it doesn't I gotta ask you to share your thoughts in Astro Lounge to receive a much proper response. Thanks for chiming in the PR ❤️ |
|
!coauthor |
|
* Apply changes from withastro#8763 * Reword Nesting Layouts translation * Fix a wrong link in layouts tutorial
* Apply changes from #8763 * Reword Nesting Layouts translation * Fix a wrong link in layouts tutorial Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com>
* update translation #9333 * update translation #8763 * start from scratch * finish translation * fix links one link is not available yet in german language * nicer * even nicer nicer * Update src/content/docs/de/basics/layouts.mdx Co-authored-by: Junseong Park <39112954+jsparkdev@users.noreply.github.com> * remove TODO comment * Update layouts.mdx --------- Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com> Co-authored-by: Junseong Park <39112954+jsparkdev@users.noreply.github.com>
Description (required)
Related issues & labels (optional)