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

@rspress/plugin-twoslash

集成 Twoslash 的 Rspress 插件,用于自动生成带有类型信息的丰富代码块。

安装

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

使用

1. 注册插件

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. 使用 Twoslash 编写代码块

在 TypeScript 代码块中使用特殊注释来启用 Twoslash 功能。

更详细的用法,请参考 Twoslash 文档。

提取类型

结果
语法
const 
const hi: "Hello"
hi
= 'Hello';
const
const msg: "Hello, world"
msg
= `${
const hi: "Hello"
hi
}, world`;

自动补全

结果
语法
var console: Console
console
.e
  • error
;

高亮显示

结果
语法
function 
function add(a: number, b: number): number
add
(
a: number
a
: number,
b: number
b
: number) {
return
a: number
a
+
b: number
b
;
}

错误提示

结果
语法
const str: string = 1;
Type 'number' is not assignable to type 'string'.

配置

该插件接受以下类型的配置对象:

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

explicitTrigger

  • 类型:boolean
  • 默认值:true

控制 Twoslash 处理哪些 TypeScript 代码块。设为 false 时处理所有 TypeScript 代码块,设为 true 时只处理带有 twoslash 标记的代码块。

cache

  • 类型:boolean
  • 默认值:true

调用 createTwoslasher 时,按编译器选项缓存 TypeScript 语言服务器。

twoslashOptions

  • 类型:TwoslashOptions
  • 默认值:undefined

向 Twoslash 传递配置,包括 TypeScript 编译器选项。