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