close
  • English
  • @rspress/plugin-twoslash new

    Integration of Twoslash's Rspress Plugin for automatically generating rich code blocks with type information.

    Installation

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

    Usage

    1. Register the plugin

    rspress.config.ts
    import { 
    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
    } 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 more detailed usage, please refer to 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

    explicitTrigger is used to configure whether to explicitly trigger the Twoslash feature. Default is true.

    • If set to false, all TypeScript code blocks will be processed by default.
    • If set to true, only code blocks with the twoslash tag will be processed.

    cache

    cache is used to cache the TypeScript language servers based on compiler options when calling createTwoslasher. Default is true.

    twoslashOptions

    twoslashOptions is used to pass options to Twoslash. This allows you to customize the Twoslash behavior, including TypeScript compiler options and other settings.