From eb05a13ac01f817e096269f98affcb7cf06c448e Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 31 Jan 2024 16:23:35 +0100 Subject: [PATCH 1/7] feat: improve stro:content types --- .changeset/young-bulldogs-tickle.md | 5 ++ packages/astro/client.d.ts | 4 ++ packages/astro/content-types.template.d.ts | 49 ----------------- packages/astro/src/virtual-modules/content.ts | 52 +++++++++++++++++++ packages/astro/tsconfig.json | 1 + 5 files changed, 62 insertions(+), 49 deletions(-) create mode 100644 .changeset/young-bulldogs-tickle.md create mode 100644 packages/astro/src/virtual-modules/content.ts diff --git a/.changeset/young-bulldogs-tickle.md b/.changeset/young-bulldogs-tickle.md new file mode 100644 index 000000000000..3d99d7563bc5 --- /dev/null +++ b/.changeset/young-bulldogs-tickle.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Improves the types for the `astro:content` module by making `defineCollection` and `z` exports available without running `astro sync` diff --git a/packages/astro/client.d.ts b/packages/astro/client.d.ts index 8542faa471a9..9ac0341c646b 100644 --- a/packages/astro/client.d.ts +++ b/packages/astro/client.d.ts @@ -160,6 +160,10 @@ declare module 'astro:components' { export * from 'astro/components'; } +declare module 'astro:content' { + export * from 'astro/virtual-modules/content.js' +} + type MD = import('./dist/@types/astro.js').MarkdownInstance>; interface ExportedMarkdownModuleEntities { frontmatter: MD['frontmatter']; diff --git a/packages/astro/content-types.template.d.ts b/packages/astro/content-types.template.d.ts index 146c52e73e10..e0d084840561 100644 --- a/packages/astro/content-types.template.d.ts +++ b/packages/astro/content-types.template.d.ts @@ -9,8 +9,6 @@ declare module 'astro:content' { } declare module 'astro:content' { - export { z } from 'astro/zod'; - type Flatten = T extends { [K: string]: infer U } ? U : never; export type CollectionKey = keyof AnyEntryMap; @@ -19,53 +17,6 @@ declare module 'astro:content' { export type ContentCollectionKey = keyof ContentEntryMap; export type DataCollectionKey = keyof DataEntryMap; - // This needs to be in sync with ImageMetadata - export type ImageFunction = () => import('astro/zod').ZodObject<{ - src: import('astro/zod').ZodString; - width: import('astro/zod').ZodNumber; - height: import('astro/zod').ZodNumber; - format: import('astro/zod').ZodUnion< - [ - import('astro/zod').ZodLiteral<'png'>, - import('astro/zod').ZodLiteral<'jpg'>, - import('astro/zod').ZodLiteral<'jpeg'>, - import('astro/zod').ZodLiteral<'tiff'>, - import('astro/zod').ZodLiteral<'webp'>, - import('astro/zod').ZodLiteral<'gif'>, - import('astro/zod').ZodLiteral<'svg'>, - import('astro/zod').ZodLiteral<'avif'>, - ] - >; - }>; - - type BaseSchemaWithoutEffects = - | import('astro/zod').AnyZodObject - | import('astro/zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> - | import('astro/zod').ZodDiscriminatedUnion - | import('astro/zod').ZodIntersection; - - type BaseSchema = - | BaseSchemaWithoutEffects - | import('astro/zod').ZodEffects; - - export type SchemaContext = { image: ImageFunction }; - - type DataCollectionConfig = { - type: 'data'; - schema?: S | ((context: SchemaContext) => S); - }; - - type ContentCollectionConfig = { - type?: 'content'; - schema?: S | ((context: SchemaContext) => S); - }; - - type CollectionConfig = ContentCollectionConfig | DataCollectionConfig; - - export function defineCollection( - input: CollectionConfig - ): CollectionConfig; - type AllValuesOf = T extends any ? T[keyof T] : never; type ValidContentEntrySlug = AllValuesOf< ContentEntryMap[C] diff --git a/packages/astro/src/virtual-modules/content.ts b/packages/astro/src/virtual-modules/content.ts new file mode 100644 index 000000000000..d0d93ad0516c --- /dev/null +++ b/packages/astro/src/virtual-modules/content.ts @@ -0,0 +1,52 @@ +import { defineCollection as _defineCollection } from '../content/runtime.js'; +export { z } from 'astro/zod'; + +// This needs to be in sync with ImageMetadata +export type ImageFunction = () => import('astro/zod').ZodObject<{ + src: import('astro/zod').ZodString; + width: import('astro/zod').ZodNumber; + height: import('astro/zod').ZodNumber; + format: import('astro/zod').ZodUnion< + [ + import('astro/zod').ZodLiteral<'png'>, + import('astro/zod').ZodLiteral<'jpg'>, + import('astro/zod').ZodLiteral<'jpeg'>, + import('astro/zod').ZodLiteral<'tiff'>, + import('astro/zod').ZodLiteral<'webp'>, + import('astro/zod').ZodLiteral<'gif'>, + import('astro/zod').ZodLiteral<'svg'>, + import('astro/zod').ZodLiteral<'avif'>, + ] + >; +}>; + +// @ts-ignore complains about circular dependency but used to work in the types template +type BaseSchemaWithoutEffects = + | import('astro/zod').AnyZodObject + | import('astro/zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> + | import('astro/zod').ZodDiscriminatedUnion + | import('astro/zod').ZodIntersection; + +type BaseSchema = + | BaseSchemaWithoutEffects + | import('astro/zod').ZodEffects; + +export type SchemaContext = { image: ImageFunction }; + +type DataCollectionConfig = { + type: 'data'; + schema?: S | ((context: SchemaContext) => S); +}; + +type ContentCollectionConfig = { + type?: 'content'; + schema?: S | ((context: SchemaContext) => S); +}; + +type CollectionConfig = ContentCollectionConfig | DataCollectionConfig; + +export function defineCollection( + input: CollectionConfig +): CollectionConfig { + return _defineCollection(input); +} diff --git a/packages/astro/tsconfig.json b/packages/astro/tsconfig.json index a9172654793b..3f37149ee113 100644 --- a/packages/astro/tsconfig.json +++ b/packages/astro/tsconfig.json @@ -4,6 +4,7 @@ "compilerOptions": { "allowJs": true, "declarationDir": "./dist", + "rootDir": ".", "outDir": "./dist", "jsx": "preserve", "types": ["@types/dom-view-transitions"] From e8631f6209ac629e500f88ae97a2728013dec1a1 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 31 Jan 2024 16:33:29 +0100 Subject: [PATCH 2/7] fix: do not change tsconfig --- packages/astro/tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/astro/tsconfig.json b/packages/astro/tsconfig.json index 3f37149ee113..a9172654793b 100644 --- a/packages/astro/tsconfig.json +++ b/packages/astro/tsconfig.json @@ -4,7 +4,6 @@ "compilerOptions": { "allowJs": true, "declarationDir": "./dist", - "rootDir": ".", "outDir": "./dist", "jsx": "preserve", "types": ["@types/dom-view-transitions"] From 1f3ea7024e4482e4684027476efb9a656d06bcff Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 31 Jan 2024 16:17:34 +0000 Subject: [PATCH 3/7] "astro/zod" -> "zod" --- packages/astro/src/virtual-modules/content.ts | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/astro/src/virtual-modules/content.ts b/packages/astro/src/virtual-modules/content.ts index d0d93ad0516c..27a0bafa0976 100644 --- a/packages/astro/src/virtual-modules/content.ts +++ b/packages/astro/src/virtual-modules/content.ts @@ -1,35 +1,35 @@ import { defineCollection as _defineCollection } from '../content/runtime.js'; -export { z } from 'astro/zod'; +export { z } from 'zod'; // This needs to be in sync with ImageMetadata -export type ImageFunction = () => import('astro/zod').ZodObject<{ - src: import('astro/zod').ZodString; - width: import('astro/zod').ZodNumber; - height: import('astro/zod').ZodNumber; - format: import('astro/zod').ZodUnion< +export type ImageFunction = () => import('zod').ZodObject<{ + src: import('zod').ZodString; + width: import('zod').ZodNumber; + height: import('zod').ZodNumber; + format: import('zod').ZodUnion< [ - import('astro/zod').ZodLiteral<'png'>, - import('astro/zod').ZodLiteral<'jpg'>, - import('astro/zod').ZodLiteral<'jpeg'>, - import('astro/zod').ZodLiteral<'tiff'>, - import('astro/zod').ZodLiteral<'webp'>, - import('astro/zod').ZodLiteral<'gif'>, - import('astro/zod').ZodLiteral<'svg'>, - import('astro/zod').ZodLiteral<'avif'>, + import('zod').ZodLiteral<'png'>, + import('zod').ZodLiteral<'jpg'>, + import('zod').ZodLiteral<'jpeg'>, + import('zod').ZodLiteral<'tiff'>, + import('zod').ZodLiteral<'webp'>, + import('zod').ZodLiteral<'gif'>, + import('zod').ZodLiteral<'svg'>, + import('zod').ZodLiteral<'avif'>, ] >; }>; // @ts-ignore complains about circular dependency but used to work in the types template type BaseSchemaWithoutEffects = - | import('astro/zod').AnyZodObject - | import('astro/zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> - | import('astro/zod').ZodDiscriminatedUnion - | import('astro/zod').ZodIntersection; + | import('zod').AnyZodObject + | import('zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> + | import('zod').ZodDiscriminatedUnion + | import('zod').ZodIntersection; type BaseSchema = | BaseSchemaWithoutEffects - | import('astro/zod').ZodEffects; + | import('zod').ZodEffects; export type SchemaContext = { image: ImageFunction }; From 006dfcca06e94faf86135ff665b6370f98b108d6 Mon Sep 17 00:00:00 2001 From: lilnasy <69170106+lilnasy@users.noreply.github.com> Date: Wed, 31 Jan 2024 16:36:16 +0000 Subject: [PATCH 4/7] `CollectionConfig` parameter extends `BaseSchema` --- packages/astro/src/virtual-modules/content.ts | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/astro/src/virtual-modules/content.ts b/packages/astro/src/virtual-modules/content.ts index 27a0bafa0976..76afad2c068d 100644 --- a/packages/astro/src/virtual-modules/content.ts +++ b/packages/astro/src/virtual-modules/content.ts @@ -1,35 +1,35 @@ import { defineCollection as _defineCollection } from '../content/runtime.js'; -export { z } from 'zod'; +import { z } from 'astro/zod'; +export { z }; // This needs to be in sync with ImageMetadata -export type ImageFunction = () => import('zod').ZodObject<{ - src: import('zod').ZodString; - width: import('zod').ZodNumber; - height: import('zod').ZodNumber; - format: import('zod').ZodUnion< +export type ImageFunction = () => z.ZodObject<{ + src: z.ZodString; + width: z.ZodNumber; + height: z.ZodNumber; + format: z.ZodUnion< [ - import('zod').ZodLiteral<'png'>, - import('zod').ZodLiteral<'jpg'>, - import('zod').ZodLiteral<'jpeg'>, - import('zod').ZodLiteral<'tiff'>, - import('zod').ZodLiteral<'webp'>, - import('zod').ZodLiteral<'gif'>, - import('zod').ZodLiteral<'svg'>, - import('zod').ZodLiteral<'avif'>, + z.ZodLiteral<'png'>, + z.ZodLiteral<'jpg'>, + z.ZodLiteral<'jpeg'>, + z.ZodLiteral<'tiff'>, + z.ZodLiteral<'webp'>, + z.ZodLiteral<'gif'>, + z.ZodLiteral<'svg'>, + z.ZodLiteral<'avif'>, ] >; }>; -// @ts-ignore complains about circular dependency but used to work in the types template type BaseSchemaWithoutEffects = - | import('zod').AnyZodObject - | import('zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> - | import('zod').ZodDiscriminatedUnion - | import('zod').ZodIntersection; + | z.AnyZodObject + | z.ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> + | z.ZodDiscriminatedUnion + | z.ZodIntersection; type BaseSchema = | BaseSchemaWithoutEffects - | import('zod').ZodEffects; + | z.ZodEffects; export type SchemaContext = { image: ImageFunction }; @@ -43,7 +43,7 @@ type ContentCollectionConfig = { schema?: S | ((context: SchemaContext) => S); }; -type CollectionConfig = ContentCollectionConfig | DataCollectionConfig; +type CollectionConfig = ContentCollectionConfig | DataCollectionConfig; export function defineCollection( input: CollectionConfig From 8506868c10c6d8a4e58b88331f9b1481ac7016a5 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Wed, 31 Jan 2024 17:40:17 +0100 Subject: [PATCH 5/7] fix: update import to zod --- packages/astro/src/virtual-modules/content.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/virtual-modules/content.ts b/packages/astro/src/virtual-modules/content.ts index 76afad2c068d..c885ebe3368c 100644 --- a/packages/astro/src/virtual-modules/content.ts +++ b/packages/astro/src/virtual-modules/content.ts @@ -1,5 +1,6 @@ import { defineCollection as _defineCollection } from '../content/runtime.js'; -import { z } from 'astro/zod'; +import { z } from 'zod'; + export { z }; // This needs to be in sync with ImageMetadata From f661e84ba58b10c4d43f7e292307ef86b1ebec13 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 1 Feb 2024 11:44:32 +0100 Subject: [PATCH 6/7] feat: add fallbacks for every export --- packages/astro/src/virtual-modules/content.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/virtual-modules/content.ts b/packages/astro/src/virtual-modules/content.ts index c885ebe3368c..a3e9a68289ae 100644 --- a/packages/astro/src/virtual-modules/content.ts +++ b/packages/astro/src/virtual-modules/content.ts @@ -28,9 +28,7 @@ type BaseSchemaWithoutEffects = | z.ZodDiscriminatedUnion | z.ZodIntersection; -type BaseSchema = - | BaseSchemaWithoutEffects - | z.ZodEffects; +type BaseSchema = BaseSchemaWithoutEffects | z.ZodEffects; export type SchemaContext = { image: ImageFunction }; @@ -51,3 +49,27 @@ export function defineCollection( ): CollectionConfig { return _defineCollection(input); } + +const noop: (...args: any[]) => any = () => {}; +/** Run `astro sync` to generate high fidelity types */ +export const getEntryBySlug = noop; +/** Run `astro sync` to generate high fidelity types */ +export const getDataEntryById = noop; +/** Run `astro sync` to generate high fidelity types */ +export const getCollection = noop; +/** Run `astro sync` to generate high fidelity types */ +export const getEntry = noop; +/** Run `astro sync` to generate high fidelity types */ +export const getEntries = noop; +/** Run `astro sync` to generate high fidelity types */ +export const reference = noop; +/** Run `astro sync` to generate high fidelity types */ +export type CollectionKey = any; +/** Run `astro sync` to generate high fidelity types */ +export type CollectionEntry = any; +/** Run `astro sync` to generate high fidelity types */ +export type ContentCollectionKey = any; +/** Run `astro sync` to generate high fidelity types */ +export type DataCollectionKey = any; +/** Run `astro sync` to generate high fidelity types */ +export type ContentConfig = any; From 02e47d085faaefa99964d1fa07fa452bc609a0ac Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 5 Feb 2024 09:43:52 -0600 Subject: [PATCH 7/7] Update .changeset/young-bulldogs-tickle.md --- .changeset/young-bulldogs-tickle.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/young-bulldogs-tickle.md b/.changeset/young-bulldogs-tickle.md index 3d99d7563bc5..7f9fcc08acbb 100644 --- a/.changeset/young-bulldogs-tickle.md +++ b/.changeset/young-bulldogs-tickle.md @@ -2,4 +2,4 @@ "astro": patch --- -Improves the types for the `astro:content` module by making `defineCollection` and `z` exports available without running `astro sync` +Improves the types for the `astro:content` module by making low fidelity types available before running `astro sync`