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

Unable to import type for SidebarItemConfig in a typescript Docusaurus project. #9902

Closed
4 of 7 tasks
jharrell opened this issue Feb 29, 2024 · 1 comment
Closed
4 of 7 tasks
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: question This issue is a user error/misunderstanding.

Comments

@jharrell
Copy link

Have you read the Contributing Guidelines on issues?

Prerequisites

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

Description

Hi there! I have two sidebar slices that have the same category.

{
  type: "category",
  label: "Platform",
  collapsed: false,
  collapsible: false,
  items: [
    'platform/a',
    'platform/b',
    {
      type: "category",
      label: "c",
      collapsed: false,
      items: [{ type: 'autogenerated', dirName: '500-platform/c' }]
    },
    'platform/d',
    'platform/e'
  ],
};

I assigned this category to a new const and dropped it into both sidebar slices, but then got an error is not assignable to type 'SidebarItemConfig'. (full error not posted, it's a big ugly TS error)

I asked on Discord and looked through examples and saw the following:

/**
 * @typedef {import('@docusaurus/plugin-content-docs').SidebarsConfig} SidebarsConfig
 * @typedef {import('@docusaurus/plugin-content-docs/lib/sidebars/types').SidebarItemConfig} SidebarItemConfig
 */

/** @type {SidebarsConfig} */

I tried to migrate that to TypeScript:

import type { SidebarItemConfig } from '@docusaurus/plugin-content-docs/lib/sidebars/types'

and got the following error:

⚠ Error (TS2307)  | 
Cannot find module '@docusaurus/plugin-content-docs/lib/sidebars/types' or its corresponding type declarations.

If possible, could this module issue be resolved, or could SidebarItemConfig be exported in a different way? Thank you!

Reproducible demo

No response

Steps to reproduce

  1. create a properly formatted category sidebar item and assign it to a variable
  2. Use said category sidebar item in one or more sidebar slices
  3. attempt to import SidebarItemConfig via JS or TS methodologies

Expected behavior

SidebarItemConfig should be usable in projects

Actual behavior

SidebarItemConfig is unable to be imported due to TypeScript module / type declaration issues.

Your environment

  • Docusaurus version used: 3.1.1
  • Environment name and version (e.g. Chrome 89, Node.js 16.4): latest Chrome, Node 20
  • Operating system and version (e.g. Ubuntu 20.04.2 LTS): MacOS Sonoma

Self-service

  • I'd be willing to fix this bug myself.
@jharrell jharrell added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Feb 29, 2024
@slorber slorber added closed: question This issue is a user error/misunderstanding. and removed status: needs triage This issue has not been triaged by maintainers labels Feb 29, 2024
@slorber
Copy link
Collaborator

slorber commented Feb 29, 2024

Hey

This is more a question than a bug to me.

import type { SidebarItemConfig } from '@docusaurus/plugin-content-docs/lib/sidebars/types'

As far as I know, we never documented anywhere to add such import. Even if it worked, it is importing internal code and might break anytime if we refactor types.


Note that in any case, you could use TypeScript type system to "extract" the types you need. Adding an extra type export does not this ability: it only makes it more convenient.

import {SidebarsConfig} from '@docusaurus/plugin-content-docs';

type SidebarConfig = SidebarsConfig[string];
type OnlyArray<Type> = Type extends unknown[] ? Type : never;
type SidebarItemConfigArray = OnlyArray<SidebarConfig>;
type SidebarItemConfig = SidebarItemConfigArray[number];

I still don't understand why you need to import that type exactly. Please provide a repro showing the problem with the TS error you encounter, and we might eventually decide to export it to make it more convenient.

I assigned this category to a new const and dropped it into both sidebar slices, but then got an error is not assignable to type 'SidebarItemConfig'. (full error not posted, it's a big ugly TS error)

I'm not able to understand what you mean here without seeing a repro, or at least a full sidebar file example including the error messages you encounter. We'll eventually reopen if you provide enough info to understand better the problem and judge whether it's worth improving something.

My intuition is that as const could also do the trick in your case, but I can't be sure without seeing the code.

@slorber slorber closed this as not planned Won't fix, can't repro, duplicate, stale Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: question This issue is a user error/misunderstanding.
Projects
None yet
Development

No branches or pull requests

2 participants