Skip to content

Commit 037495d

Browse files
Thodor12ascorbic
andauthored
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 <[email protected]>
1 parent 23881e7 commit 037495d

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

.changeset/sharp-clouds-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Added additional generated typings for the content layer

packages/astro/templates/content/types.d.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ declare module 'astro:content' {
3131
ContentEntryMap[C]
3232
>['slug'];
3333

34+
export type ReferenceDataEntry<C extends CollectionKey, E extends keyof DataEntryMap[C] = string> = {
35+
collection: C;
36+
id: E;
37+
}
38+
export type ReferenceContentEntry<C extends keyof ContentEntryMap, E extends ValidContentEntrySlug<C> | (string & {}) = string> = {
39+
collection: C;
40+
slug: E;
41+
}
42+
3443
/** @deprecated Use `getEntry` instead. */
3544
export function getEntryBySlug<
3645
C extends keyof ContentEntryMap,
@@ -61,19 +70,13 @@ declare module 'astro:content' {
6170
export function getEntry<
6271
C extends keyof ContentEntryMap,
6372
E extends ValidContentEntrySlug<C> | (string & {}),
64-
>(entry: {
65-
collection: C;
66-
slug: E;
67-
}): E extends ValidContentEntrySlug<C>
73+
>(entry: ReferenceContentEntry<C, E>): E extends ValidContentEntrySlug<C>
6874
? Promise<CollectionEntry<C>>
6975
: Promise<CollectionEntry<C> | undefined>;
7076
export function getEntry<
7177
C extends keyof DataEntryMap,
7278
E extends keyof DataEntryMap[C] | (string & {}),
73-
>(entry: {
74-
collection: C;
75-
id: E;
76-
}): E extends keyof DataEntryMap[C]
79+
>(entry: ReferenceDataEntry<C, E>): E extends keyof DataEntryMap[C]
7780
? Promise<DataEntryMap[C][E]>
7881
: Promise<CollectionEntry<C> | undefined>;
7982
export function getEntry<
@@ -99,16 +102,10 @@ declare module 'astro:content' {
99102

100103
/** Resolve an array of entry references from the same collection */
101104
export function getEntries<C extends keyof ContentEntryMap>(
102-
entries: {
103-
collection: C;
104-
slug: ValidContentEntrySlug<C>;
105-
}[],
105+
entries: ReferenceContentEntry<C, ValidContentEntrySlug<C>>[],
106106
): Promise<CollectionEntry<C>[]>;
107107
export function getEntries<C extends keyof DataEntryMap>(
108-
entries: {
109-
collection: C;
110-
id: keyof DataEntryMap[C];
111-
}[],
108+
entries: ReferenceDataEntry<C, keyof DataEntryMap[C]>[],
112109
): Promise<CollectionEntry<C>[]>;
113110

114111
export function render<C extends keyof AnyEntryMap>(
@@ -120,14 +117,8 @@ declare module 'astro:content' {
120117
): import('astro/zod').ZodEffects<
121118
import('astro/zod').ZodString,
122119
C extends keyof ContentEntryMap
123-
? {
124-
collection: C;
125-
slug: ValidContentEntrySlug<C>;
126-
}
127-
: {
128-
collection: C;
129-
id: keyof DataEntryMap[C];
130-
}
120+
? ReferenceContentEntry<C, ValidContentEntrySlug<C>>
121+
: ReferenceDataEntry<C, keyof DataEntryMap[C]>
131122
>;
132123
// Allow generic `string` to avoid excessive type errors in the config
133124
// if `dev` is not running to update as you edit.

0 commit comments

Comments
 (0)