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:
parent
23881e7164
commit
037495d437
2 changed files with 20 additions and 24 deletions
5
.changeset/sharp-clouds-begin.md
Normal file
5
.changeset/sharp-clouds-begin.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Added additional generated typings for the content layer
|
39
packages/astro/templates/content/types.d.ts
vendored
39
packages/astro/templates/content/types.d.ts
vendored
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue