docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

Typescript compilation error after swizzling

Open JontyMC opened this issue 3 years ago • 4 comments

Have you read the Contributing Guidelines on issues?

Prerequisites

  • [X] I'm using the latest version of Docusaurus.
  • [X] I have tried the npm run clear or yarn clear command.
  • [X] I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • [X] I have tried creating a repro with https://new.docusaurus.io.
  • [X] I have read the console error message carefully (if applicable).

Description

After swizzling a component (eject + typescript), the project does not compile.

Reproducible demo

No response

Steps to reproduce

  1. Create new typescript docusaurus npx create-docusaurus@latest my-website classic --typescript
  2. cd .\my-website\
  3. Swizzle component npm run swizzle @docusaurus/theme-classic DocSidebarItem/Link -- --typescript --eject
  4. Compile npm run typecheck

Expected behavior

No errors

Actual behavior

src/theme/DocSidebarItem/Link/index.tsx:4:35 - error TS2307: Cannot find module '@docusaurus/theme-common/internal' or its corresponding type declarations.

import {isActiveSidebarItem} from '@docusaurus/theme-common/internal';

Your environment

  • Public source code:
  • Public site URL:
  • Docusaurus version used: 2.1.0
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): node v16.14.0
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): Windows 10

Self-service

  • [ ] I'd be willing to fix this bug myself.

JontyMC avatar Oct 19 '22 15:10 JontyMC

Hey,

It looks to me it is related to the package.json "exports" that we introduced recently.

Try this:

{
  "extends": "@tsconfig/docusaurus/tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".",
+  "moduleResolution": "Node16"
  }
}

Probably related to: https://github.com/tsconfig/bases/pull/122

slorber avatar Oct 19 '22 16:10 slorber

Thanks that worked

JontyMC avatar Oct 19 '22 18:10 JontyMC

It seems like using Node16 for module resolution makes my Swizzle work but then causes some of my type imports to fail (Docusaurus 2.2.0):

import type {
  Sidebars,
  SidebarItemDoc,
} from '@docusaurus/plugin-content-docs/src/sidebars/types';

erickzhao avatar Dec 16 '22 23:12 erickzhao

@erickzhao we never documented anywhere add such type imports. Whenever you import from /src or /lib you tap into internals. I'm not against exposing more things publicly but we'd rather discuss your use-case for importing these on a separate issue.

slorber avatar Dec 22 '22 11:12 slorber

We hit a similar issue when importing from @theme/Tabs in a custom page. There are no TS errors but the types of the props are not loaded for autocomplete (because they delegate to @docusaurus/theme-common/internal). Adding "moduleResolution": "Node16" to our tsconfig fixes it.

image

targos avatar Feb 07 '23 11:02 targos