Fumadocs
Integrations

Twoslash

Use Typescript Twoslash in your docs

Usage

Thanks to the Twoslash integration of Shiki, the default code syntax highlighter, it is as simple as adding a transformer.

npm install fumadocs-twoslash twoslash

Update your serverExternalPackages in Next.js config:

import { createMDX } from 'fumadocs-mdx/next';
 
const config = {
  reactStrictMode: true,
  serverExternalPackages: ['typescript', 'twoslash'],
};
 
const withMDX = createMDX();
 
export default withMDX(config);

Add to your Shiki transformers.

source.config.ts (Fumadocs MDX)
import {  } from 'fumadocs-mdx/config';
import {  } from 'fumadocs-twoslash';
import {  } from 'fumadocs-core/mdx-plugins';
 
export default ({
  : {
    : {
      : {
        : 'github-light',
        : 'github-dark',
      },
      : [
        ...(. ?? []),
        (),
      ],
    },
  },
});

Add required styles, Tailwind CSS is recommended.

Tailwind CSS
@import 'fumadocs-twoslash/twoslash.css';

Add MDX components.

mdx-components.tsx
import { Popup, PopupContent, PopupTrigger } from 'fumadocs-twoslash/ui';
import defaultComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';
 
export function getMDXComponents(components?: MDXComponents): MDXComponents {
  return {
    ...defaultComponents,
    Popup,
    PopupContent,
    PopupTrigger,
    ...components,
  };
}

Now you can add twoslash meta string to codeblocks.

```ts twoslash
console.log('Hello World');
```

Example

Learn more about Twoslash notations.

Test
.g
  • group
  • groupCollapsed
  • groupEnd
;
const
const player: Player
player
: = { : 'Hello World' };
const  = '123';
 
.();
 
import {  } from 'fumadocs-openapi';
 
void ({
  : ['./museum.yaml'],
  : './content/docs/ui',
});
const  = '123';
 
a = 132;
Cannot assign to 'a' because it is a constant.

Cache

You can enable filesystem cache with typesCache option:

source.config.ts
import {  } from 'fumadocs-twoslash';
import {  } from 'fumadocs-twoslash/cache-fs';
 
({
  : (),
});

How is this guide?

On this page