Skip to content

Installing

Twoslash Features

FeatureSupported Status
JSDocs and Type Hover boxes
Error Handling/Messages
Type Extraction
Code Completions
Code Highlighting
Code Cutting
Callouts
TSComiler Overrides
Show Emitted Files⛔️

Installation

Install the package with your favorite package manager

Terminal window
npm i expressive-code-twoslash

Add to EC Config

Add ecTwoSlash to your Expressive Code plugin list

import {
function defineConfig(config: AstroUserConfig): AstroUserConfig

See the full Astro Configuration API Documentation https://astro.build/config

defineConfig
} from "astro/config";
import
function astroExpressiveCode(integrationOptions?: AstroExpressiveCodeOptions): {
name: string;
hooks: {
"astro:config:setup": (args: unknown) => Promise<void>;
};
}

Astro integration that adds Expressive Code support to code blocks in Markdown & MDX documents.

astroExpressiveCode
from 'astro-expressive-code'
import
function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin

Add Twoslash support to your Expressive Code TypeScript code blocks.

@paramoptions - Configuration options for the plugin.

@paramoptions.explicitTrigger - Settings for the explicit trigger.

@paramoptions.languages - The languages to apply this transformer to.

@paramoptions.includeJsDoc - If true, includes JSDoc comments in the hover popup.

@paramoptions.twoslashOptions - Options to forward to twoslash.

@seehttps://twoslash.matthiesen.dev for the full documentation.

@returnsA plugin object with the specified configuration.

ecTwoSlash
from "expressive-code-twoslash";
// https://astro.build/config
export default
function defineConfig(config: AstroUserConfig): AstroUserConfig

See the full Astro Configuration API Documentation https://astro.build/config

defineConfig
({
AstroUserConfig.integrations?: (false | AstroIntegration | (false | AstroIntegration | null | undefined)[] | null | undefined)[]

@nameintegrations

@description

Extend Astro with custom integrations. Integrations are your one-stop-shop for adding framework support (like Solid.js), new features (like sitemaps), and new libraries (like Partytown).

Read our Integrations Guide for help getting started with Astro Integrations.

import react from '@astrojs/react';
import tailwind from '@astrojs/tailwind';
{
// Example: Add React + Tailwind support to Astro
integrations: [react(), tailwind()]
}

integrations
: [
function astroExpressiveCode(integrationOptions?: AstroExpressiveCodeOptions): {
name: string;
hooks: {
"astro:config:setup": (args: unknown) => Promise<void>;
};
}

Astro integration that adds Expressive Code support to code blocks in Markdown & MDX documents.

astroExpressiveCode
({
plugins?: (ExpressiveCodePlugin | ExpressiveCodePlugin[])[] | undefined

An optional array of plugins that should be used when rendering code blocks.

To add a plugin, import its initialization function and call it inside this array.

If the plugin has any configuration options, you can pass them to the initialization function as an object containing your desired property values.

If any nested arrays are found inside the plugins array, they will be flattened before processing.

plugins
: [
function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin

Add Twoslash support to your Expressive Code TypeScript code blocks.

@paramoptions - Configuration options for the plugin.

@paramoptions.explicitTrigger - Settings for the explicit trigger.

@paramoptions.languages - The languages to apply this transformer to.

@paramoptions.includeJsDoc - If true, includes JSDoc comments in the hover popup.

@paramoptions.twoslashOptions - Options to forward to twoslash.

@seehttps://twoslash.matthiesen.dev for the full documentation.

@returnsA plugin object with the specified configuration.

ecTwoSlash
()],
}),
],
});

Config Options

Default config options shown.

function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin

Add Twoslash support to your Expressive Code TypeScript code blocks.

@paramoptions - Configuration options for the plugin.

@paramoptions.explicitTrigger - Settings for the explicit trigger.

@paramoptions.languages - The languages to apply this transformer to.

@paramoptions.includeJsDoc - If true, includes JSDoc comments in the hover popup.

@paramoptions.twoslashOptions - Options to forward to twoslash.

@seehttps://twoslash.matthiesen.dev for the full documentation.

@returnsA plugin object with the specified configuration.

ecTwoSlash
({
PluginTwoslashOptions.explicitTrigger?: boolean | RegExp

If true, requires twoslash to be present in the code block meta for this transformer to be applied.

If a RegExp, requires the RegExp to match a directive in the code block meta for this transformer to be applied.

If false, this transformer will be applied to all code blocks that match the specified languages.

It is recommended to keep this as true to avoid unnecessary processing.

@defaulttrue

explicitTrigger
: true,
PluginTwoslashOptions.includeJsDoc?: boolean

If true, includes JSDoc comments in the hover popup.

@defaulttrue

includeJsDoc
: true,
PluginTwoslashOptions.allowNonStandardJsDocTags?: boolean

Allows non-standard JSDoc tags to be included in the hover popup.

Non-standard tags are tags that are not included in the default JSDoc tag list.

@example @customTag, @docs, @omglookatthis

@defaultfalse

allowNonStandardJsDocTags
: false,
PluginTwoslashOptions.languages?: readonly string[]

The languages to apply this transformer to.

@default["ts", "tsx"]

languages
: ["ts", "tsx"],
PluginTwoslashOptions.twoslashOptions?: TwoslashOptions

Options to forward to twoslash.

@default{}

twoslashOptions
: {},
})