Skip to content

Commit 12c6834

Browse files
authored
fix: Don’t require getCollection() filter to be a type guard (#5998)
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>
1 parent 5e3d538 commit 12c6834

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

.changeset/rotten-dogs-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Update `getCollection()` filter to support type guards _or_ unknown values

packages/astro/src/content/internal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function createGetCollection({
4040
collectionToEntryMap: CollectionToEntryMap;
4141
collectionToRenderEntryMap: CollectionToEntryMap;
4242
}) {
43-
return async function getCollection(collection: string, filter?: () => boolean) {
43+
return async function getCollection(collection: string, filter?: (entry: any) => unknown) {
4444
const lazyImports = Object.values(collectionToEntryMap[collection] ?? {});
4545
const entries = Promise.all(
4646
lazyImports.map(async (lazyImport) => {

packages/astro/src/content/template/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ declare module 'astro:content' {
4848
collection: C,
4949
filter?: (entry: CollectionEntry<C>) => entry is E
5050
): Promise<E[]>;
51+
export function getCollection<C extends keyof typeof entryMap>(
52+
collection: C,
53+
filter?: (entry: CollectionEntry<C>) => unknown
54+
): Promise<CollectionEntry<C>[]>;
5155

5256
type InferEntrySchema<C extends keyof typeof entryMap> = import('astro/zod').infer<
5357
Required<ContentConfig['collections'][C]>['schema']

0 commit comments

Comments
 (0)