summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2023-01-25 17:21:27 -0500
committerGravatar GitHub <noreply@github.com> 2023-01-25 16:21:27 -0600
commitdabce6b8c684f851c3535f8acead06cbef6dce2a (patch)
treea163018ada39a53987a2888c3c3cdd701dc88857
parentb53e0717b7f6b042baaeec7f87999e99c76c031c (diff)
downloadastro-dabce6b8c684f851c3535f8acead06cbef6dce2a.tar.gz
astro-dabce6b8c684f851c3535f8acead06cbef6dce2a.tar.zst
astro-dabce6b8c684f851c3535f8acead06cbef6dce2a.zip
[Content collections] Support type guards on `getCollection()` filter (#5970)
* fix: respect type guards from collection filter * chore: changeset
-rw-r--r--.changeset/strong-hotels-sort.md5
-rw-r--r--packages/astro/src/content/template/types.d.ts6
2 files changed, 8 insertions, 3 deletions
diff --git a/.changeset/strong-hotels-sort.md b/.changeset/strong-hotels-sort.md
new file mode 100644
index 000000000..7b674aef5
--- /dev/null
+++ b/.changeset/strong-hotels-sort.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Add type guard support to filters on `getCollection()`
diff --git a/packages/astro/src/content/template/types.d.ts b/packages/astro/src/content/template/types.d.ts
index 81ea9a95e..628f5d18d 100644
--- a/packages/astro/src/content/template/types.d.ts
+++ b/packages/astro/src/content/template/types.d.ts
@@ -44,10 +44,10 @@ declare module 'astro:content' {
): E extends ValidEntrySlug<C>
? Promise<CollectionEntry<C>>
: Promise<CollectionEntry<C> | undefined>;
- export function getCollection<C extends keyof typeof entryMap>(
+ export function getCollection<C extends keyof typeof entryMap, E extends CollectionEntry<C>>(
collection: C,
- filter?: (data: CollectionEntry<C>) => boolean
- ): Promise<CollectionEntry<C>[]>;
+ filter?: (entry: CollectionEntry<C>) => entry is E
+ ): Promise<E[]>;
type InferEntrySchema<C extends keyof typeof entryMap> = import('astro/zod').infer<
Required<ContentConfig['collections'][C]>['schema']