mdx example results in type errors
Opened this issue · 3 comments
airtonix commented
Following the quickstart for mdx here https://www.content-collections.dev/docs/content/mdx
results in this:
But if i workaround it with a asserted type predicate then it's satisfied.
Did I miss something?
const me = defineCollection({
name: 'me',
directory: 'content/data/me',
include: '**/*.mdx',
schema: (z) => ({
title: z.string(),
description: z.string().optional(),
}),
transform: async (document, context) => {
assert(isDocumentWithContent(document), 'Document has no content');
const mdx = await compileMDX(context, document);
return {
...document,
mdx,
};
},
});
/**
* Hack Predicate to override aggressive generics
* done by content-collections/core
*/
function isDocumentWithContent<T extends { content: string }>(
document: any
): document is T {
return typeof document?.content === 'string';
}
function assert(condition: any, message: string): asserts condition {
if (!condition) {
throw new Error(message);
}
}
I still get the error if I describe a content: z.string()
in the schema:
I imagine something is going awry here :
type AddContent<
TShape extends ZodRawShape
> = TShape extends {
content: ZodTypeAny;
}
? TShape
: TShape & WithContent;
type GetParsedShape<
TParser extends Parser,
TShape extends ZodRawShape
> = Parsers[TParser]["hasContent"] extends true
? AddContent<TShape>
: TShape;
type GetShape<
TParser extends Parser | undefined,
TShape extends ZodRawShape
> = TParser extends Parser
? GetParsedShape<TParser, TShape>
: AddContent<TShape>;
type Schema<
TParser extends Parser | undefined,
TShape extends ZodRawShape
> = z$1.infer<ZodObject<GetShape<TParser, TShape>>>
& {
_meta: Meta;
};
sdorra commented
I am currently on vacation. I'll take a look at it next week.
airtonix commented
I am currently on vacation. I'll take a look at it next week.
All good, no one expects you to deal with these issues straight away 👍🏻 (or even ever).
We have a workaround for now, so not a high priority