mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
fix: Don’t require getCollection()
filter to be a type guard (#5998)
Commit dabce6b8c6
(#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>
This commit is contained in:
parent
5e3d538812
commit
12c68343c0
3 changed files with 10 additions and 1 deletions
5
.changeset/rotten-dogs-hide.md
Normal file
5
.changeset/rotten-dogs-hide.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
Update `getCollection()` filter to support type guards _or_ unknown values
|
|
@ -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) => {
|
||||
|
|
|
@ -48,6 +48,10 @@ declare module 'astro:content' {
|
|||
collection: C,
|
||||
filter?: (entry: CollectionEntry<C>) => entry is E
|
||||
): Promise<E[]>;
|
||||
export function getCollection<C extends keyof typeof entryMap>(
|
||||
collection: C,
|
||||
filter?: (entry: CollectionEntry<C>) => unknown
|
||||
): Promise<CollectionEntry<C>[]>;
|
||||
|
||||
type InferEntrySchema<C extends keyof typeof entryMap> = import('astro/zod').infer<
|
||||
Required<ContentConfig['collections'][C]>['schema']
|
||||
|
|
Loading…
Reference in a new issue