Closed teinett closed 2 weeks ago
I'm guessing that post.data.tags
is not being strictly typed? It looks like there is an | undefined
in the type definition, which is probably what's causing this TypeScript error.
Not sure if we should widen our getEntries
type to allow this loose behavior or if there's a way we can fix the post.data.tags
typing.
@natemoo-re I was looking into this and I'm not sure if there is a way to fix the typing of post.data.tags
. It looks to me as if data
is typed as Record<string, any>
and I don't know if this could be changed safely.
I would open a PR where I add undefined
to getEntries with error handling if undefined is actually passed
Would post.data.tags!
fix it too? It's a bit odd that we loosen the type to allow undefined
, and then we error in runtime again. If post.data.tags
exists for sure, then the error should be fixed at the type-level first.
You missed to check if post is undefined, see example in doc https://docs.astro.build/en/guides/content-collections/#building-for-server-output-ssr
Try this:
const { post } = Astro.props;
if (post === undefined) {
throw new Error("Post is required"); // or: return Astro.redirect('/');
}
Hi @teinett, I'm taking a look at this, but your reproduction repo no longer exists. If this is still a problem, could you create a new, minimal reproduction and I'll take a look. Thanks!
Hello @teinett. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro
will be closed if they have no activity within 3 days.
Hi @teinett, I'm taking a look at this, but your reproduction repo no longer exists. If this is still a problem, could you create a new, minimal reproduction and I'll take a look. Thanks!
Reproduction repo is public again: https://github.com/teinett/learn-astro-images
It looks like the issue is that tags
is optional in the config, which means the type is nullable. If I remove optional()
then the error goes away and the type is inferred correctly. I suppose getEntries
could be expanded to allow undefined to be passed (and return undefined for that), but I think iot's probably better to keep the null check on the property itself when it's explicitly optional.
I think this is behaving as expected
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
I have a page of one post and want to show all tags from this post:
src/pages/posts/[slug].astro
I receive an error in VS Code:
Screenshot:
What's the expected result?
I follow official docs: https://docs.astro.build/en/guides/content-collections/#accessing-referenced-data I expect to have no errors.
Link to Minimal Reproducible Example
https://github.com/teinett/learn-astro-images
Participation