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

mdx example results in type errors #359

Open
airtonix opened this issue Oct 19, 2024 · 3 comments
Open

mdx example results in type errors #359

airtonix opened this issue Oct 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@airtonix
Copy link

airtonix commented Oct 19, 2024

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
Copy link
Owner

sdorra commented Oct 19, 2024

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

@airtonix
Copy link
Author

airtonix commented Oct 19, 2024

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
Copy link
Owner

sdorra commented Oct 21, 2024

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

@sdorra sdorra added the bug Something isn't working label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants