0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

feat: content layer typings (#12666)

* Add additional typings for referenced content collection entries

* Add changeset

* Default typings to string for easier use

---------

Co-authored-by: Matt Kane <m@mk.gg>
This commit is contained in:
Thom van den Akker 2025-01-30 11:33:37 +01:00 committed by GitHub
parent 23881e7164
commit 037495d437
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 24 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Added additional generated typings for the content layer

View file

@ -31,6 +31,15 @@ declare module 'astro:content' {
ContentEntryMap[C] ContentEntryMap[C]
>['slug']; >['slug'];
export type ReferenceDataEntry<C extends CollectionKey, E extends keyof DataEntryMap[C] = string> = {
collection: C;
id: E;
}
export type ReferenceContentEntry<C extends keyof ContentEntryMap, E extends ValidContentEntrySlug<C> | (string & {}) = string> = {
collection: C;
slug: E;
}
/** @deprecated Use `getEntry` instead. */ /** @deprecated Use `getEntry` instead. */
export function getEntryBySlug< export function getEntryBySlug<
C extends keyof ContentEntryMap, C extends keyof ContentEntryMap,
@ -61,19 +70,13 @@ declare module 'astro:content' {
export function getEntry< export function getEntry<
C extends keyof ContentEntryMap, C extends keyof ContentEntryMap,
E extends ValidContentEntrySlug<C> | (string & {}), E extends ValidContentEntrySlug<C> | (string & {}),
>(entry: { >(entry: ReferenceContentEntry<C, E>): E extends ValidContentEntrySlug<C>
collection: C;
slug: E;
}): E extends ValidContentEntrySlug<C>
? Promise<CollectionEntry<C>> ? Promise<CollectionEntry<C>>
: Promise<CollectionEntry<C> | undefined>; : Promise<CollectionEntry<C> | undefined>;
export function getEntry< export function getEntry<
C extends keyof DataEntryMap, C extends keyof DataEntryMap,
E extends keyof DataEntryMap[C] | (string & {}), E extends keyof DataEntryMap[C] | (string & {}),
>(entry: { >(entry: ReferenceDataEntry<C, E>): E extends keyof DataEntryMap[C]
collection: C;
id: E;
}): E extends keyof DataEntryMap[C]
? Promise<DataEntryMap[C][E]> ? Promise<DataEntryMap[C][E]>
: Promise<CollectionEntry<C> | undefined>; : Promise<CollectionEntry<C> | undefined>;
export function getEntry< export function getEntry<
@ -99,16 +102,10 @@ declare module 'astro:content' {
/** Resolve an array of entry references from the same collection */ /** Resolve an array of entry references from the same collection */
export function getEntries<C extends keyof ContentEntryMap>( export function getEntries<C extends keyof ContentEntryMap>(
entries: { entries: ReferenceContentEntry<C, ValidContentEntrySlug<C>>[],
collection: C;
slug: ValidContentEntrySlug<C>;
}[],
): Promise<CollectionEntry<C>[]>; ): Promise<CollectionEntry<C>[]>;
export function getEntries<C extends keyof DataEntryMap>( export function getEntries<C extends keyof DataEntryMap>(
entries: { entries: ReferenceDataEntry<C, keyof DataEntryMap[C]>[],
collection: C;
id: keyof DataEntryMap[C];
}[],
): Promise<CollectionEntry<C>[]>; ): Promise<CollectionEntry<C>[]>;
export function render<C extends keyof AnyEntryMap>( export function render<C extends keyof AnyEntryMap>(
@ -120,14 +117,8 @@ declare module 'astro:content' {
): import('astro/zod').ZodEffects< ): import('astro/zod').ZodEffects<
import('astro/zod').ZodString, import('astro/zod').ZodString,
C extends keyof ContentEntryMap C extends keyof ContentEntryMap
? { ? ReferenceContentEntry<C, ValidContentEntrySlug<C>>
collection: C; : ReferenceDataEntry<C, keyof DataEntryMap[C]>
slug: ValidContentEntrySlug<C>;
}
: {
collection: C;
id: keyof DataEntryMap[C];
}
>; >;
// Allow generic `string` to avoid excessive type errors in the config // Allow generic `string` to avoid excessive type errors in the config
// if `dev` is not running to update as you edit. // if `dev` is not running to update as you edit.