Twoslash
TypeScript type information in code blocks.
Twoslash runs TypeScript on a code block when the site builds, and shows a name's type and doc comment in a popup on hover. Hover one below:
import { } from "vuesho/client";
const { , } = ();Setup
Install the Packages
Twoslash needs TypeScript 6's JavaScript API, which TypeScript 7 doesn't have. twoslash-vue adds Vue support:
npm i -D @vuesho/twoslash twoslash-vue typescript@6Add the Plugin
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";
import { twoslashPlugin } from "@vuesho/twoslash";
const docs = defineDocs();
export default createSite(docs.toSource()).plugins(
recommendedPreset(),
twoslashPlugin(),
);The plugin adds the popup styles, and works with any highlighter.
Write a Code Block
Add twoslash after the language of a ts, tsx, js, jsx, json or vue code block. Imports in the block resolve from the project root:
```ts twoslash
const greeting = "Hello";
```Notations
A Twoslash comment changes what the block shows, and doesn't appear in the output:
| Notation | Result |
|---|---|
// ^? | Shows the type of the name above the caret |
// ^| | Shows the completions at the caret |
// ^^^ | Highlights the code above the carets |
// @log: text | Adds a message under the line above, and @warn, @error or @annotate adds one in another style |
// ---cut--- | Hides the code above it, which is still type-checked |
// @filename: a.ts | Starts another file, which the next files can import |
// @strict: false | Sets a tsconfig.json option for the block |
// @showEmit | Shows the JavaScript that TypeScript emits, instead of the code |
import { } from "vuesho/client";
const href = ("/guide");Adds the site's base path.();
.slic- slice
;
See the Twoslash notation reference for every notation.
Errors
A type error fails the build at its line. // @errors lists the error codes to expect and show in the block, and // @noErrors hides every error:
const count: number = "one";Reusing Code
To reuse code in a later block on the page:
include <name>after the language saves the block's code.// @include: <name>inserts it into a later block.- A
// - <part>line marks a part, and// @include: <name>-<part>inserts only the lines above it.
const = { : "Alice" };
// - user
const = { ..., : "admin" };user.;Vue Code Blocks
A vue block is type-checked as a single-file component, including its <template>. A tsx or jsx block is checked as Vue JSX:
<script setup lang="ts">
import { } from "vuesho/client";
const { } = ();
</script>
<template>
< v-if="">The page is dark.</>
</template>Options
twoslashPlugin() takes the options of Shiki's Twoslash transformer, like processHoverInfo:
Prop
Type
Caching
twoslashFileCache() saves each block's types in node_modules/.vuesho/twoslash, or in the folder of dir, so the next build and vuesho dev read them instead of running TypeScript:
import { twoslashFileCache, twoslashPlugin } from "@vuesho/twoslash";
createSite(docs.toSource()).plugins(
recommendedPreset(),
twoslashPlugin({ typesCache: twoslashFileCache() }),
);An edit of a block's code runs it again, but an edit of a type it imports or an upgrade of a dependency does not. After either, clear the folder before the next build:
rm -rf node_modules/.vuesho/twoslashCustom Popups
To change how the popups open, place your own TwoslashPopups.vue in src/components/content/. It receives the HTML of the block's popups, and in the code block, the button with the ID `${id}-${i}` opens popup i:
Prop
Type
This component opens each popup on click, in a Reka UI popover:
<script setup lang="ts">
import { onMounted, shallowRef } from "vue";
import { PopoverAnchor, PopoverContent, PopoverPortal, PopoverRoot } from "reka-ui";
const props = defineProps<{ id: string; popups: string[] }>();
const open = shallowRef<{ html: string; trigger: HTMLElement }>();
onMounted(() => {
props.popups.forEach((html, i) => {
const trigger = document.getElementById(`${props.id}-${i}`);
trigger?.addEventListener("click", () => (open.value = { html, trigger }));
});
});
</script>
<template>
<PopoverRoot :open="!!open" @update:open="(value) => !value && (open = undefined)">
<PopoverAnchor :reference="open?.trigger" />
<PopoverPortal>
<PopoverContent class="fd-twoslash-popover" side="bottom" :side-offset="4">
<div v-html="open?.html" />
</PopoverContent>
</PopoverPortal>
</PopoverRoot>
</template>fd-twoslash-popover applies the default style to a popup. For popups that open on hover, use Popup, PopupTrigger and PopupContent from @vuesho/twoslash/ui.