Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeScript schema error when using project references #1777

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-mice-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes an issue where TypeScript could fail to serialize the frontmatter schema when configured to emit declaration files
13 changes: 10 additions & 3 deletions packages/starlight/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ type BaseSchemaWithoutEffects =
type BaseSchema = BaseSchemaWithoutEffects | z.ZodEffects<BaseSchemaWithoutEffects>;

/** Type that extends Starlight’s default schema with an optional, user-defined schema. */
type ExtendedSchema<T extends BaseSchema> = T extends BaseSchema
type ExtendedSchema<T extends BaseSchema | never = never> = [T] extends [never]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is used to disable distributivity as when conditional types act on a generic type, they become distributive when given a union type which can be the case here.

? DefaultSchema
: T extends BaseSchema
? z.ZodIntersection<DefaultSchema, T>
: DefaultSchema;

Expand Down Expand Up @@ -147,8 +149,13 @@ interface DocsSchemaOpts<T extends BaseSchema> {
}

/** Content collection schema for Starlight’s `docs` collection. */
export function docsSchema<T extends BaseSchema>({ extend }: DocsSchemaOpts<T> = {}) {
return (context: SchemaContext): ExtendedSchema<T> => {
export function docsSchema<T extends BaseSchema | never = never>(
...args: [DocsSchemaOpts<T>?]
): (context: SchemaContext) => ExtendedSchema<T> {
const [options = {}] = args;
const { extend } = options;

return (context: SchemaContext) => {
const UserSchema = typeof extend === 'function' ? extend(context) : extend;

return (
Expand Down
Loading