Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-cameras-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fix `defineLiveCollection()` so `LiveLoader` data types declared as interfaces are accepted.
2 changes: 1 addition & 1 deletion packages/astro/src/content/loaders/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface LoadCollectionContext<TCollectionFilter = unknown> {
}

export interface LiveLoader<
TData extends Record<string, any> = Record<string, unknown>,
TData extends Record<string, any> = Record<string, any>,
TEntryFilter extends Record<string, any> | never = never,
TCollectionFilter extends Record<string, any> | never = never,
TError extends Error = Error,
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/types/public/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export interface CacheHint {
lastModified?: Date;
}

export interface LiveDataEntry<TData extends Record<string, any> = Record<string, unknown>> {
export interface LiveDataEntry<TData extends Record<string, any> = Record<string, any>> {
/** The ID of the entry. Unique per collection. */
id: string;
/** The parsed entry data */
Expand All @@ -175,14 +175,14 @@ export interface LiveDataEntry<TData extends Record<string, any> = Record<string
cacheHint?: CacheHint;
}

export interface LiveDataCollection<TData extends Record<string, any> = Record<string, unknown>> {
export interface LiveDataCollection<TData extends Record<string, any> = Record<string, any>> {
entries: Array<LiveDataEntry<TData>>;
/** A hint for how to cache this collection. Individual entries can also have cache hints */
cacheHint?: CacheHint;
}

export interface LiveDataCollectionResult<
TData extends Record<string, any> = Record<string, unknown>,
TData extends Record<string, any> = Record<string, any>,
TError extends Error = Error,
> {
entries?: Array<LiveDataEntry<TData>>;
Expand All @@ -191,7 +191,7 @@ export interface LiveDataCollectionResult<
}

export interface LiveDataEntryResult<
TData extends Record<string, any> = Record<string, unknown>,
TData extends Record<string, any> = Record<string, any>,
TError extends Error = Error,
> {
entry?: LiveDataEntry<TData>;
Expand Down
36 changes: 36 additions & 0 deletions packages/astro/test/types/define-live-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it } from 'node:test';
import { expectTypeOf } from 'expect-type';
import { defineLiveCollection } from 'astro/content/config';
import type { LiveLoader } from 'astro/loaders';

function assertType<T>(data: T, cb: (data: NoInfer<T>) => void) {
cb(data);
}

interface Data {
body: string;
}

const loader: LiveLoader<Data> = {
name: 'test-loader',
loadEntry: async () => ({
id: 'hello-world',
data: { body: 'Hello world' },
}),
loadCollection: async () => ({
entries: [
{
id: 'hello-world',
data: { body: 'Hello world' },
},
],
}),
};

describe('defineLiveCollection()', () => {
it('accepts live loaders whose data type is declared as an interface', () => {
assertType(defineLiveCollection({ loader }), (config) => {
expectTypeOf(config.loader).toEqualTypeOf<LiveLoader<Data>>();
});
});
});
Loading