For AI agents: the complete documentation index is available at https://rspress.rs/llms.txt, the full documentation bundle is available at https://rspress.rs/llms-full.txt, and this page is available as Markdown at https://rspress.rs/plugin/official-plugins/twoslash.md.
close
  • English
  • @rspress/plugin-twoslash

    Integrates Twoslash with Rspress to generate rich TypeScript code blocks with type information.

    Installation

    npm
    yarn
    pnpm
    bun
    deno
    vlt
    npm add @rspress/plugin-twoslash -D

    Usage

    1. Register the plugin

    rspress.config.ts
    import { 
    function defineConfig<const Config extends UserConfig>(config: () => Promise<Config>): UserConfigAsyncFn (+1 overload)

    Define an async Rspress configuration function. Use this overload when you need to perform async operations during configuration, such as fetching remote data, reading files, or dynamically generating config.

    @paramconfig - An async function that returns the Rspress configuration.@returnsThe same async function (enables type checking and IDE autocompletion).@example
    import { defineConfig } from '@rspress/core';
    
    export default defineConfig(async () => {
      const remoteConfig = await fetchConfig();
      return {
        title: remoteConfig.title,
      };
    });
    defineConfig
    } from '@rspress/core';
    import {
    function pluginTwoslash(options?: PluginTwoslashOptions): RspressPlugin

    Plugin to applies Twoslash transformations to code blocks.

    pluginTwoslash
    } from '@rspress/plugin-twoslash';
    export default
    function defineConfig(config: UserConfig): UserConfig (+1 overload)

    Define a static Rspress configuration object.

    @paramconfig - The Rspress configuration object.@returnsThe same configuration object (enables type checking and IDE autocompletion).@example
    import { defineConfig } from '@rspress/core';
    
    export default defineConfig({
      title: 'My Site',
    });
    defineConfig
    ({
    UserConfig.plugins?: RspressPlugin[] | undefined

    Doc plugins

    plugins
    : [
    function pluginTwoslash(options?: PluginTwoslashOptions): RspressPlugin

    Plugin to applies Twoslash transformations to code blocks.

    pluginTwoslash
    ()],
    });

    2. Write code blocks with twoslash

    Use special comments within TypeScript code blocks to enable Twoslash features.

    For detailed usage, see the Twoslash documentation.

    Extract type

    Rendered
    Syntax
    const 
    const hi: "Hello"
    hi
    = 'Hello';
    const
    const msg: "Hello, world"
    msg
    = `${
    const hi: "Hello"
    hi
    }, world`;

    Completions

    Rendered
    Syntax
    var console: Console
    console
    .e
    • error
    ;

    Highlighting

    Rendered
    Syntax
    function 
    function add(a: number, b: number): number
    add
    (
    a: number
    a
    : number,
    b: number
    b
    : number) {
    return
    a: number
    a
    +
    b: number
    b
    ;
    }

    Error

    Rendered
    Syntax
    const str: string = 1;
    Type 'number' is not assignable to type 'string'.

    Config

    The plugin accepts an object with the following type:

    export interface PluginTwoslashOptions {
      
    PluginTwoslashOptions.explicitTrigger?: boolean | undefined
    explicitTrigger
    ?: boolean;
    PluginTwoslashOptions.cache?: boolean | undefined
    cache
    ?: boolean;
    PluginTwoslashOptions.twoslashOptions?: TwoslashOptions | undefined
    twoslashOptions
    ?: TwoslashOptions;
    }

    explicitTrigger

    • Type: boolean
    • Default: true

    Controls which TypeScript code blocks Twoslash processes. Set it to false to process all TypeScript code blocks, or to true to process only blocks marked twoslash.

    cache

    • Type: boolean
    • Default: true

    Caches TypeScript language servers by compiler options when calling createTwoslasher.

    twoslashOptions

    • Type: TwoslashOptions
    • Default: undefined

    Passes options to Twoslash, including TypeScript compiler options.