summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Thom van den Akker <thodor20@gmail.com> 2025-01-30 11:33:37 +0100
committerGravatar GitHub <noreply@github.com> 2025-01-30 10:33:37 +0000
commit037495d437d2328bf10ffadc22cc114ccf474c65 (patch)
tree0f9877f0c12f7653de91e55e2c40e7dcd845524f
parent23881e716457c358c8adf929a62f52237cef27cd (diff)
downloadastro-037495d437d2328bf10ffadc22cc114ccf474c65.tar.gz
astro-037495d437d2328bf10ffadc22cc114ccf474c65.tar.zst
astro-037495d437d2328bf10ffadc22cc114ccf474c65.zip
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>
-rw-r--r--.changeset/sharp-clouds-begin.md5
-rw-r--r--packages/astro/templates/content/types.d.ts39
2 files changed, 20 insertions, 24 deletions
diff --git a/.changeset/sharp-clouds-begin.md b/.changeset/sharp-clouds-begin.md
new file mode 100644
index 000000000..12b857a5a
--- /dev/null
+++ b/.changeset/sharp-clouds-begin.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Added additional generated typings for the content layer
diff --git a/packages/astro/templates/content/types.d.ts b/packages/astro/templates/content/types.d.ts
index 14b57053c..30f4286b8 100644
--- a/packages/astro/templates/content/types.d.ts
+++ b/packages/astro/templates/content/types.d.ts
@@ -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.