Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include undefined in types for getEntry with content layer #12601

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Dec 3, 2024

Changes

Currently the types for getEntry exclude undefined if the key passed is a key in the type. This works for the old system where there were types for every entry, but now they are types as Record<string, {//...the type }>, it means any key will match (because they're all strings), meaning the entries are incorrectly returned as not nullable. This PR changes this so that if the key type is string (i.e. the type does not have specific entries), then the return type is nullable.

This is technically a breaking change but I think we can slip it in in 5.0.x

Fixes #12598

Testing

Docs

Our migration guide already says that the types for getEntry have been loosened.

Copy link

changeset-bot bot commented Dec 3, 2024

🦋 Changeset detected

Latest commit: 3546e72

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Dec 3, 2024
Copy link
Member

@HiDeoo HiDeoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally against the repro in #12598 and this now works as expected:

image

🙌

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — thanks @ascorbic!

@ascorbic ascorbic merged commit 0724929 into main Dec 3, 2024
14 checks passed
@ascorbic ascorbic deleted the content-entry-type-undef branch December 3, 2024 16:16
@astrobot-houston astrobot-houston mentioned this pull request Dec 3, 2024
@Nickersoft
Copy link

Nickersoft commented Dec 12, 2024

Just ran into this myself using Content Collections – I think it might be worth updating the docs, as the way they're currently written imply that you don't need any null/undefined checks when using getEntry:

https://docs.astro.build/en/guides/content-collections/#rendering-body-content

Also, what is the proper way to perform these checks? Right now I'm just using a not-null assertion when passing it into render().

@ascorbic
Copy link
Contributor Author

You can pass null entries to render(): it will render an empty component. I think the answer is to ensure the types allow that. But the correct way to handle it is probably to do something like render a 404 if it's not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getEntry type issue in backwards compatibility mode
4 participants