Skip to content

Typescript compilation error after swizzling #8226

Closed

Description

@JontyMC

Have you read the Contributing Guidelines on issues?

Prerequisites

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.

Activity

  1. added
    bug
    status: needs triage
    on Oct 19, 2022
  2. slorber commented on Oct 19, 2022

    @slorber
    Collaborator

    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: tsconfig/bases#122

  3. removed
    status: needs triage
    on Oct 19, 2022
  4. JontyMC commented on Oct 19, 2022

    @JontyMC
    Author

    Thanks that worked

  5. erickzhao commented on Dec 16, 2022

    @erickzhao
    Contributor

    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';
  6. slorber commented on Dec 22, 2022

    @slorber
    Collaborator

    @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.

  7. targos commented on Feb 7, 2023

    @targos

    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

  8. added a commit that references this issue on May 20, 2023
  9. added a commit that references this issue on May 30, 2023
  10. added a commit that references this issue on Jun 2, 2023
  11. added a commit that references this issue on Jun 20, 2023
  12. added a commit that references this issue on Jun 20, 2023
  13. added a commit that references this issue on Jun 29, 2023
  14. added a commit that references this issue on Jul 12, 2023
  15. added a commit that references this issue on Nov 5, 2023
  16. added a commit that references this issue on Dec 17, 2023
  17. added a commit that references this issue on Jun 9, 2026
  18. slorber commented on Oct 16, 2026

    @slorber
    Collaborator

    This no longer reproduces on Docusaurus 4.0.0-canary-6972 (TypeScript 7.0.2), nor on 3.10.2 (TypeScript 6.0.3). With the reported steps (swizzle @docusaurus/theme-classic DocSidebarItem/Link -- --typescript --eject), npm run typecheck passes.

    The error came from the v2 tsconfig (@tsconfig/docusaurus), whose default moduleResolution ignores package.json "exports", so subpaths like @docusaurus/theme-common/internal could not be found. Since Docusaurus 3.0, the TypeScript template extends @docusaurus/tsconfig (#9050), which uses "moduleResolution": "bundler" (#9258) and resolves these subpaths.

    Repro: https://github.com/slorber/docusaurus-repros/tree/issue-8226

    • Minimal 4.0.0-canary-6972 TypeScript site using the template's tsconfig.json (extends @docusaurus/tsconfig)
    • DocSidebarItem/Link, DocSidebarItem/Category and Tabs ejected with --typescript; they import @docusaurus/plugin-content-docs/client and @docusaurus/theme-common/internal
    • A page using @theme/Tabs, with a @ts-expect-error proving its props are typed and not any (the case from the comments)
    • npm run typecheck exits with 0 errors, and the site builds

    I also re-ran the same swizzle on 2.1.0 with @tsconfig/[email protected] and TypeScript 4.8.4: the TS2307 error from the issue still shows there. If you upgraded an older site and kept a tsconfig that extends @tsconfig/docusaurus, switch it to "extends": "@docusaurus/tsconfig" as in the current template.

    (AI-assisted)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions