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

* fix: respect type guards from collection filter

* chore: changeset
This commit is contained in:
Ben Holmes 2023-01-25 17:21:27 -05:00 committed by GitHub
parent b53e0717b7
commit dabce6b8c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Add type guard support to filters on `getCollection()`

View file

@ -44,10 +44,10 @@ declare module 'astro:content' {
): E extends ValidEntrySlug<C> ): E extends ValidEntrySlug<C>
? Promise<CollectionEntry<C>> ? Promise<CollectionEntry<C>>
: Promise<CollectionEntry<C> | undefined>; : 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, collection: C,
filter?: (data: CollectionEntry<C>) => boolean filter?: (entry: CollectionEntry<C>) => entry is E
): Promise<CollectionEntry<C>[]>; ): Promise<E[]>;
type InferEntrySchema<C extends keyof typeof entryMap> = import('astro/zod').infer< type InferEntrySchema<C extends keyof typeof entryMap> = import('astro/zod').infer<
Required<ContentConfig['collections'][C]>['schema'] Required<ContentConfig['collections'][C]>['schema']