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

[Content collections] Support type guards on getCollection() filter #5970

Merged
merged 2 commits into from
Jan 25, 2023

Conversation

bholmesdev
Copy link
Contributor

Changes

  • Add type guard support for getCollection() filters

Testing

Manual testing

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Jan 24, 2023

🦋 Changeset detected

Latest commit: 18f6ea3

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 Jan 24, 2023
@natemoo-re natemoo-re merged commit dabce6b into main Jan 25, 2023
@natemoo-re natemoo-re deleted the fix/collection-filter-generic branch January 25, 2023 22:21
@astrobot-houston astrobot-houston mentioned this pull request Jan 25, 2023
matthewp pushed a commit that referenced this pull request Jan 26, 2023
…#5970)

* fix: respect type guards from collection filter

* chore: changeset
@andersk
Copy link
Contributor

andersk commented Jan 26, 2023

This broke the use of plain boolean filters.

/home/anders/zulip/zulip-blog/src/pages/author/[...author].astro:22:44 Error: Argument of type '(post: CollectionEntry<"posts">) => boolean' is not assignable to parameter of type '(entry: CollectionEntry<"posts">) => entry is CollectionEntry<"posts">'.
  Signature '(post: CollectionEntry<"posts">): boolean' must be a type predicate.
22  const posts = await getCollection("posts", (post) => post.data.author === author);
                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It also doesn’t conceptually make sense to require a type guard. A type guard (entry) => entry is E is expected to return true when given an E, and also return false when not given an E (documentation), so it can only be used to filter for all members of E.

This is why TypeScript arrays have two filter overloads.

andersk added a commit to andersk/astro that referenced this pull request Jan 26, 2023
Commit dabce6b (withastro#5970) broke the use
of a plain boolean filter.  Add an overload similar to TypeScript’s
Array#filter overload:

https://github.com/microsoft/TypeScript/blob/v4.9.4/lib/lib.es5.d.ts#L1442-L1453

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
andersk added a commit to andersk/astro that referenced this pull request Jan 26, 2023
Commit dabce6b (withastro#5970) broke the use
of a plain boolean filter.  Add an overload similar to TypeScript’s
Array#filter overload:

https://github.com/microsoft/TypeScript/blob/v4.9.4/lib/lib.es5.d.ts#L1442-L1453

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
andersk added a commit to andersk/astro that referenced this pull request Jan 26, 2023
Commit dabce6b (withastro#5970) broke the use
of a plain boolean filter.  Add an overload similar to TypeScript’s
Array#filter overload:

https://github.com/microsoft/TypeScript/blob/v4.9.4/lib/lib.es5.d.ts#L1442-L1453

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
andersk added a commit to andersk/astro that referenced this pull request Jan 26, 2023
Commit dabce6b (withastro#5970) broke the use
of a plain boolean filter.  Add an overload similar to TypeScript’s
Array#filter overload:

https://github.com/microsoft/TypeScript/blob/v4.9.4/lib/lib.es5.d.ts#L1442-L1453

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
bholmesdev pushed a commit that referenced this pull request Jan 27, 2023
Commit dabce6b (#5970) broke the use
of a plain boolean filter.  Add an overload similar to TypeScript’s
Array#filter overload:

https://github.com/microsoft/TypeScript/blob/v4.9.4/lib/lib.es5.d.ts#L1442-L1453

Signed-off-by: Anders Kaseorg <andersk@mit.edu>

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
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.

3 participants