Overview
Twoslash provides:- Type Hovering: Hover over variables to see their types
- Inline Errors: TypeScript errors displayed in code
- Query Comments: Show types with
// ^?queries - Completions: Autocomplete suggestions with
// ^| - Type Highlighting: Highlight specific tokens
Installation
Install the Twoslash package:Configuration
Basic Setup
source.config.ts
Advanced Configuration
Usage
Enable Twoslash
WithexplicitTrigger: true (recommended), add twoslash to the meta string:
counthas typenumberresulthas typestring
Type Errors
TypeScript errors are displayed inline:Completions
Show autocomplete suggestions with^|:
foo, bar, toString, valueOf, etc.
Multi-file Setup
Define types in virtual files:Compiler Options
Set TypeScript compiler options per block:Implementation Details
Transformer Architecture
From/packages/twoslash/src/index.ts:27:
Lazy Loading
Twoslash is lazily loaded to work on serverless platforms (from/packages/twoslash/src/index.ts:31):
Custom Components
Fumadocs uses custom JSX components for rendering (from/packages/twoslash/src/index.ts:48):
<Popup>: Container for hover interactions<PopupContent>: The tooltip content<PopupTrigger>: The hoverable token
Markdown Rendering
JSDoc comments are rendered as Markdown (from/packages/twoslash/src/index.ts:121):
UI Components
Twoslash includes pre-built UI components:Styling
Default Styles
Twoslash components use specific class names:Custom Styles
Configure class names via renderer options:Advanced Features
Type Caching
Twoslash caches type information for performance:File System Cache
Cache to disk for faster rebuilds:Custom Compiler Options
Per-project TypeScript configuration:Virtual File System
Define shared type definitions:Examples
React Component Types
Generic Functions
Type Guards
Async/Await
Troubleshooting
Lazy Loading Issues
If Twoslash fails with lazy-loaded languages:TypeScript Errors
If you see incorrect TypeScript errors:- Check compiler options match your project
- Add
// @errors: <code>to suppress expected errors - Use
// @ts-expect-errorfor known issues
Performance Issues
Twoslash can slow down builds:- Enable explicit trigger:
explicitTrigger: true - Use file system cache
- Limit Twoslash to specific pages