Skip to content

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

Merged
natemoo-re merged 2 commits into
mainfrom
fix/collection-filter-generic
Jan 25, 2023
Merged

[Content collections] Support type guards on getCollection() filter#5970
natemoo-re merged 2 commits into
mainfrom
fix/collection-filter-generic

Conversation

@bholmesdev
Copy link
Copy Markdown
Contributor

Changes

  • Add type guard support for getCollection() filters

Testing

Manual testing

Docs

N/A

@changeset-bot
Copy link
Copy Markdown

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
Copy Markdown
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