From 6ae244d1f3e375eb65a4932551e8e77bd1706fe6 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 26 Jan 2023 12:25:01 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20Don=E2=80=99t=20require=20`getCollection?= =?UTF-8?q?()`=20filter=20to=20be=20a=20type=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit dabce6b8c684f851c3535f8acead06cbef6dce2a (#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 --- .changeset/rotten-dogs-hide.md | 5 +++++ packages/astro/src/content/internal.ts | 2 +- packages/astro/src/content/template/types.d.ts | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .changeset/rotten-dogs-hide.md diff --git a/.changeset/rotten-dogs-hide.md b/.changeset/rotten-dogs-hide.md new file mode 100644 index 000000000000..b167279aac0a --- /dev/null +++ b/.changeset/rotten-dogs-hide.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Update `getCollection()` filter to support type guards _or_ unknown values diff --git a/packages/astro/src/content/internal.ts b/packages/astro/src/content/internal.ts index 8d20aa184ba1..2002aa76d6b9 100644 --- a/packages/astro/src/content/internal.ts +++ b/packages/astro/src/content/internal.ts @@ -40,7 +40,7 @@ export function createGetCollection({ collectionToEntryMap: CollectionToEntryMap; collectionToRenderEntryMap: CollectionToEntryMap; }) { - return async function getCollection(collection: string, filter?: () => boolean) { + return async function getCollection(collection: string, filter?: (entry: any) => unknown) { const lazyImports = Object.values(collectionToEntryMap[collection] ?? {}); const entries = Promise.all( lazyImports.map(async (lazyImport) => { diff --git a/packages/astro/src/content/template/types.d.ts b/packages/astro/src/content/template/types.d.ts index 628f5d18df12..f14a541f1e8a 100644 --- a/packages/astro/src/content/template/types.d.ts +++ b/packages/astro/src/content/template/types.d.ts @@ -48,6 +48,10 @@ declare module 'astro:content' { collection: C, filter?: (entry: CollectionEntry) => entry is E ): Promise; + export function getCollection( + collection: C, + filter?: (entry: CollectionEntry) => unknown + ): Promise[]>; type InferEntrySchema = import('astro/zod').infer< Required['schema']