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

Typescript errors (cannot find module) when importing .md or .mdx files into a .tsx component #10640

Open
1 of 2 tasks
pburrows opened this issue Nov 3, 2024 · 4 comments
Open
1 of 2 tasks
Labels
closed: invalid This issue / PR is low quality, unwanted or spam and doesn't qualify as a contribution in any way. documentation The issue is related to the documentation of Docusaurus

Comments

@pburrows
Copy link

pburrows commented Nov 3, 2024

Have you read the Contributing Guidelines on issues?

Description

There is no documentation (or if there is, I cannot find it) of what needs to be modified in the tsconfig.json file to remove the "cannot find module" errors when importing .md or .mdx files into a .tsx page.

For sass modules (*.module.scss files), there is documentation on how to resolve these errors in typescript:
https://docusaurus.io/docs/styling-layout#typescript-support

I have no doubt the fix for .md and .mdx files are similar to the fix for sass, but it isn't obvious, and I haven't been able to figure out what docusaurus plugin item needs to be added to the tsconfig.json types to resolve these issues.

Examples of error:

image

Self-service

  • I'd be willing to address this documentation request myself.
@pburrows pburrows added documentation The issue is related to the documentation of Docusaurus status: needs triage This issue has not been triaged by maintainers labels Nov 3, 2024
@slorber
Copy link
Collaborator

slorber commented Nov 3, 2024

please provide a repro, we can't help without it.

You don't even share your current tsconfig

The styling docs page has a mistake, I'll fix it. If you don't, you should use @docusaurus/tsconfig, not @tsconfig/docusaurus/tsconfig.json

@slorber slorber closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2024
@slorber slorber added closed: invalid This issue / PR is low quality, unwanted or spam and doesn't qualify as a contribution in any way. and removed status: needs triage This issue has not been triaged by maintainers labels Nov 3, 2024
@pburrows
Copy link
Author

pburrows commented Nov 4, 2024

My tsconfig.json currently looks like this:

{
  // This file is not used in compilation. It is here just for a nice editor experience.
  "extends": "@docusaurus/tsconfig",
  "compilerOptions": {
    "baseUrl": ".",
    "types": [
      "docusaurus-plugin-sass",
    ]
  }
}

As for steps to reproduce... you don't really need any. Try to import a markdown document. For instance, create a brand new docusaurus project straight from the docs like this:

npx create-docusaurus@latest my-website classic --typescript

then open up the src/pages/index.tsx file and add this to the top (you don't even have to run it)

import TranslateYourSite from '../../docs/tutorial-extras/translate-your-site.md';

You should see that line highlight with a TS2307 error.

And it doesn't matter where the markdown file is. You can create an .md file in the same folder and you get the same behavior.

It's not a bug, really, it is a documentation error. Something is missing in the tsconfig.json file to properly recognize the .md and .mdx files as modules.

@Josh-Cena
Copy link
Collaborator

Indeed, I think you would need something like

declare module "*.mdx" {
  function Component(): JSX.Element;

  export default Component;
}

To make it work

@slorber
Copy link
Collaborator

slorber commented Nov 5, 2024

👍

I think we'll add this to our module type aliases, considering we already handle some similar cases:

declare module '*.svg' {
  import type {ComponentType, SVGProps} from 'react';

  const ReactComponent: ComponentType<
    SVGProps<SVGSVGElement> & {title?: string}
  >;

  export default ReactComponent;
}

declare module '*.module.css' {
  const classes: {readonly [key: string]: string};
  export default classes;
}

declare module '*.css' {
  const src: string;
  export default src;
}

Will think about it, maybe the type should also expose other things like the toc, metadata, frontmatter etc

@slorber slorber reopened this Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: invalid This issue / PR is low quality, unwanted or spam and doesn't qualify as a contribution in any way. documentation The issue is related to the documentation of Docusaurus
Projects
None yet
Development

No branches or pull requests

3 participants