Installing
Twoslash Features
Section titled “Twoslash Features”| Feature | Supported Status |
|---|---|
| JSDocs and Type Hover boxes | ✅ |
| Error Handling/Messages | ✅ |
| Type Extraction | ✅ |
| Code Completions | ✅ |
| Code Highlighting | ✅ |
| Code Cutting | ✅ |
| Callouts | ✅ |
| TS Compiler Overrides | ✅ |
| Show Emitted Files | ⛔️ |
Installation
Section titled “Installation”Install the package with your favorite package manager
npm i expressive-code-twoslashpnpm add expressive-code-twoslashyarn add expressive-code-twoslashAdd to EC Config
Section titled “Add to EC Config”Add ecTwoSlash to your Expressive Code plugin list
import { function defineConfig<const TLocales extends Locales = never, const TDriver extends SessionDriverName = never, const TFontProviders extends Array<FontProvider> = never>(config: AstroUserConfig<TLocales, TDriver, TFontProviders>): AstroUserConfig<TLocales, TDriver, TFontProviders>
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.
ecTwoSlash from "expressive-code-twoslash";
// https://astro.build/configexport default defineConfig<never, never, never>(config: AstroUserConfig<never, never, never>): AstroUserConfig<never, never, never>
See the full Astro Configuration API Documentation
https://astro.build/config
defineConfig({ AstroUserConfig<TLocales extends Locales = never, TSession extends SessionDriverName = never, TFontProviders extends Array<FontProvider> = never>.integrations?: (false | AstroIntegration | (false | AstroIntegration | null | undefined)[] | null | undefined)[] | undefined
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.
ecTwoSlash()], }), ],});import { function defineEcConfig(config: AstroExpressiveCodeOptions): AstroExpressiveCodeOptions
A utility function that helps you define an Expressive Code configuration object. It is meant
to be used inside the optional config file ec.config.mjs located in the root directory
of your Astro project, and its return value to be exported as the default export.
Expressive Code will automatically detect this file and use the exported configuration object
to override its own default settings.
Using this function is recommended, but not required. It just passes through the given object,
but it also provides type information for your editor's auto-completion and type checking.
defineEcConfig } from 'astro-expressive-code'import function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin
Add Twoslash support to your Expressive Code TypeScript code blocks.
ecTwoSlash from "expressive-code-twoslash";
export default function defineEcConfig(config: AstroExpressiveCodeOptions): AstroExpressiveCodeOptions
A utility function that helps you define an Expressive Code configuration object. It is meant
to be used inside the optional config file ec.config.mjs located in the root directory
of your Astro project, and its return value to be exported as the default export.
Expressive Code will automatically detect this file and use the exported configuration object
to override its own default settings.
Using this function is recommended, but not required. It just passes through the given object,
but it also provides type information for your editor's auto-completion and type checking.
defineEcConfig({ 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.
ecTwoSlash()],});import { function defineConfig<const TLocales extends Locales = never, const TDriver extends SessionDriverName = never, const TFontProviders extends Array<FontProvider> = never>(config: AstroUserConfig<TLocales, TDriver, TFontProviders>): AstroUserConfig<TLocales, TDriver, TFontProviders>
See the full Astro Configuration API Documentation
https://astro.build/config
defineConfig } from "astro/config";import function starlight(userOpts: StarlightUserConfigWithPlugins): AstroIntegration
starlight from "@astrojs/starlight";import function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin
Add Twoslash support to your Expressive Code TypeScript code blocks.
ecTwoSlash from "expressive-code-twoslash";
// https://astro.build/configexport default defineConfig<never, never, never>(config: AstroUserConfig<never, never, never>): AstroUserConfig<never, never, never>
See the full Astro Configuration API Documentation
https://astro.build/config
defineConfig({ AstroUserConfig<TLocales extends Locales = never, TSession extends SessionDriverName = never, TFontProviders extends Array<FontProvider> = never>.integrations?: (false | AstroIntegration | (false | AstroIntegration | null | undefined)[] | null | undefined)[] | undefined
integrations: [ function starlight(userOpts: StarlightUserConfigWithPlugins): AstroIntegration
starlight({ title: string | Record<string, string>
Title for your website. Will be used in metadata and as browser tab title.
title: "Starlight", expressiveCode?: boolean | StarlightExpressiveCodeOptions | undefined
Define how code blocks are rendered by passing options to Expressive Code,
or disable the integration by passing false.
expressiveCode: { 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.
ecTwoSlash()], }, }), ],});import { function defineEcConfig(config: StarlightExpressiveCodeOptions): StarlightExpressiveCodeOptions
A utility function that helps you define an Expressive Code configuration object. It is meant
to be used inside the optional config file ec.config.mjs located in the root directory
of your Starlight project, and its return value to be exported as the default export.
Expressive Code will automatically detect this file and use the exported configuration object
to override its own default settings.
Using this function is recommended, but not required. It just passes through the given object,
but it also provides type information for your editor's auto-completion and type checking.
defineEcConfig } from "@astrojs/starlight/expressive-code";import function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin
Add Twoslash support to your Expressive Code TypeScript code blocks.
ecTwoSlash from "expressive-code-twoslash";
export default function defineEcConfig(config: StarlightExpressiveCodeOptions): StarlightExpressiveCodeOptions
A utility function that helps you define an Expressive Code configuration object. It is meant
to be used inside the optional config file ec.config.mjs located in the root directory
of your Starlight project, and its return value to be exported as the default export.
Expressive Code will automatically detect this file and use the exported configuration object
to override its own default settings.
Using this function is recommended, but not required. It just passes through the given object,
but it also provides type information for your editor's auto-completion and type checking.
defineEcConfig({ 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.
ecTwoSlash()],});import function rehypeExpressiveCode(options?: RehypeExpressiveCodeOptions): (tree: Root, file: any) => Promise<void>
rehypeExpressiveCode from 'rehype-expressive-code'import function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin
Add Twoslash support to your Expressive Code TypeScript code blocks.
ecTwoSlash from "expressive-code-twoslash";
/** @type {import('rehype-expressive-code').RehypeExpressiveCodeOptions} */const const rehypeExpressiveCodeOptions: { plugins: ExpressiveCodePlugin[];}
rehypeExpressiveCodeOptions = { plugins: ExpressiveCodePlugin[]
plugins: [function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin
Add Twoslash support to your Expressive Code TypeScript code blocks.
ecTwoSlash()],}
// Rest of your Next.js configFor more information on how to add to your Next.js config, see the Expressive Code documentation.
Config Options
Section titled “Config Options”Default config options shown.
function ecTwoSlash(options?: PluginTwoslashOptions): ExpressiveCodePlugin
Add Twoslash support to your Expressive Code TypeScript code blocks.
ecTwoSlash({ PluginTwoslashOptions.explicitTrigger?: boolean | RegExp | undefined
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.
explicitTrigger: true, PluginTwoslashOptions.languages?: readonly string[] | undefined
The languages to apply this transformer to.
languages: ["ts", "tsx", "vue"], PluginTwoslashOptions.includeJsDoc?: boolean | undefined
If true, includes JSDoc comments in the hover popup.
includeJsDoc: true,Message: PluginTwoslashOptions.allowNonStandardJsDocTags?: boolean | undefined
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.
allowNonStandardJsDocTags: true, PluginTwoslashOptions.twoslashOptions?: TwoslashOptions | undefined
Options to forward to twoslash.
twoslashOptions: { TwoslashExecuteOptions.compilerOptions?: ts.CompilerOptions | undefined
Allows setting any of the compiler options from outside the function
compilerOptions: { ts.CompilerOptions.moduleResolution?: ts.ModuleResolutionKind | undefined
moduleResolution: 100 satisfies (alias) namespace tsimport ts
ts.enum ts.ModuleResolutionKind
ModuleResolutionKind.function (enum member) ts.ModuleResolutionKind.Bundler = 100
Bundler, ts.CompilerOptions.lib?: string[] | undefined
lib: ["lib.es2022.d.ts", "lib.dom.d.ts", "lib.dom.iterable.d.ts"], } },})