Fumadocs rebuilt on Hamur, the original is fumadocs.dev
Skip to Content
Fumadocs

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. Type information is resolved by the native TypeScript 7 compiler, from the working directory of your project.

npm install fumadocs-twoslash

Externalize typescript from the server bundle:

next.config.mjs
const config = {
  reactStrictMode: true,
  serverExternalPackages: ['typescript'],
};

transformerTwoslash is a Shiki transformer. Add it to the transformers of the rehype code plugin:

source.config.ts
import { defineConfig } from 'fumadocs-mdx/config';
import { transformerTwoslash } from 'fumadocs-twoslash';
import { rehypeCodeDefaultOptions } from 'fumadocs-core/mdx-plugins';

export default defineConfig({
  mdxOptions: {
    rehypeCodeOptions: {
      themes: {
        light: 'github-light',
        dark: 'github-dark',
      },
      transformers: [...(rehypeCodeDefaultOptions.transformers ?? []), transformerTwoslash()],
    },
  },
});

Add styles. Tailwind CSS v4 is required.

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

Add MDX components.

components/mdx.tsx
import * as Twoslash from 'fumadocs-twoslash/ui'; 
import defaultComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';

export function getMDXComponents(components?: MDXComponents) {
  return {
    ...defaultComponents,
    ...Twoslash,
    ...components,
  } satisfies MDXComponents;
}

Now you can add twoslash meta string to codeblocks.

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

Cache

Optionally, you can enable filesystem cache with typesCache option:

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

({
  : (),
});

Example

Learn more about Twoslash notations.

.g
  • group
  • groupCollapsed
  • groupEnd
;
const player: = { : 'Hello World' };
const player: Player
('test');
const  = '123';

.();
const  = '123';

a = 132;
Cannot assign to 'a' because it is a constant.

How is this guide?

Last updated on