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]
|
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.
|
||||||
|
|
Loading…
Add table
Reference in a new issue