sdorra / content-collections

Transform your content into type-safe data collections
https://content-collections.dev
MIT License
464 stars 21 forks source link

mdx example results in type errors #359

Open airtonix opened 2 days ago

airtonix commented 2 days ago

Following the quickstart for mdx here https://www.content-collections.dev/docs/content/mdx

image

results in this:

image

But if i workaround it with a asserted type predicate then it's satisfied.

image

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:

image

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 2 days ago

I am currently on vacation. I'll take a look at it next week.

airtonix commented 2 days ago

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

sdorra commented 18 hours ago

@airtonix i can't reproduce it. Which version of typescript do you use? And can you post your tsconfig?