Registry / testing / twoslash-vue

twoslash-vue

JSON →
library0.3.8jsnpmunverified

Twoslash-vue is an extension for the Twoslash library, specifically designed to bring its powerful TypeScript language server features to Vue Single File Components (SFCs). It enables rich code insights such as type on hover, compilation errors, and code completions directly within Vue `<script setup>` and `<script>` blocks, as well as the template context when supported by underlying Vue language tools. The current stable version is 0.3.8, and the package maintains a relatively active release cadence, frequently updating to support the latest TypeScript versions (currently ^5.5.0 || ^6.0.0) and integrating with advancements in the `@vue/language-core` library. Its key differentiator is its specialized focus on the complexities of Vue SFCs, providing a robust solution for developers looking to document and explain Vue code with accurate type inference and diagnostics, leveraging the established Twoslash ecosystem rather than being a standalone tool for Vue.

npm install twoslash-vue
INSTALL
IMPORT
SIG · TWOSLASH-VUE
T
twoslash-vue
testingjavascriptv0.3.8
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createTwoSlashFromVueSFC
import { createTwoSlashFromVueSFC } from 'twoslash-vue'
const createTwoSlashFromVueSFC = require('twoslash-vue')
This is the primary function for processing Vue SFC code. Twoslash-vue is an ESM-first package.
TwoSlashReturn
import type { TwoSlashReturn } from 'twoslash'
The return type for `createTwoSlashFromVueSFC` is typically imported from the core 'twoslash' package, as twoslash-vue extends its functionality.
Transformer
import type { TwoSlashTransformer } from 'twoslash-vue'
While `createTwoSlashFromVueSFC` is common, some advanced integrations might use or implement a `TwoSlashTransformer` type specific to Vue processing.

Demonstrates how to use `createTwoSlashFromVueSFC` to process a Vue SFC string and extract Twoslash diagnostics like queries, errors, and completions.

import { createTwoSlashFromVueSFC } from 'twoslash-vue'; // Example Vue SFC code demonstrating type queries and errors const vueSFC = ` <script setup lang="ts"> import { ref } from 'vue' const count = ref(0) // ^? const message: string = 'Hello Vue' // ^? function increment() { count.value++ // This would be a type error if uncommented: // message = 123; // @ts-expect-error } </script> <template> <button @click="increment">{{ message }} {{ count }}</button> </template> <style scoped> button { font-weight: bold; } </style> `; // Process the Vue SFC to extract Twoslash information const twoslashResult = createTwoSlashFromVueSFC(vueSFC, 'MyComponent.vue'); console.log('Extracted Errors:', twoslashResult.errors); console.log('Extracted Queries:', twoslashResult.queries); console.log('Extracted Completions:', twoslashResult.completions); // The 'code' property contains the TypeScript code derived from the SFC, // which Twoslash processed internally. console.log('Transformed TS Code snippet (first 200 chars):\n', twoslashResult.code.substring(0, 200) + '...'); // To visualize this, you would typically integrate with a syntax highlighter // (e.g., Shiki) and a Twoslash renderer in a documentation site.
Debug
Known issues
breakingVersion 0.3.0 introduced a breaking change by explicitly enforcing TypeScript version `^5.5.0` as a peer dependency. Projects using older TypeScript versions will encounter installation or runtime errors.
fix
Ensure your project's `typescript` dependency is updated to `^5.5.0 || ^6.0.0` or a compatible version as specified in the package's peer dependencies. Run `npm install typescript@latest` or `yarn add typescript@latest`.
affects: >=0.3.0
gotchaTwoslash-vue relies heavily on `@vue/language-core` for parsing Vue SFCs. Frequent updates to `@vue/language-core` can lead to rapid minor version bumps in `twoslash-vue` itself. It's crucial to keep `twoslash-vue` updated to ensure compatibility with the latest Vue tooling features and bug fixes.
fix
Regularly update `twoslash-vue` and its related dependencies. Consider using a `^` or `~` range for `twoslash-vue` in your `package.json` to receive compatible updates.
affects: >=0.3.0
gotchaWhile `twoslash-vue` processes Vue SFCs, the full Twoslash experience (like rendering tooltips) requires integration with a complete Twoslash renderer and a syntax highlighter like Shiki. `twoslash-vue` only provides the processing logic.
fix
Ensure you are using `twoslash-vue` in conjunction with the core `twoslash` library and a suitable renderer. Refer to the main Twoslash documentation for full integration instructions into your documentation pipeline.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Cannot find module 'typescript' or its corresponding type declarations.
`typescript` is a peer dependency and was not installed or is an incompatible version.
fix
Install `typescript` manually: `npm install typescript` or `yarn add typescript`. Verify the installed version meets `twoslash-vue`'s peer dependency requirements (e.g., `^5.5.0 || ^6.0.0`).
TypeError: createTwoSlashFromVueSFC is not a function
Attempting to `require` the module in a CommonJS environment, or incorrect named import.
fix
Ensure you are using ES Modules syntax: `import { createTwoSlashFromVueSFC } from 'twoslash-vue';`. If you are in a pure CommonJS environment, you might need a bundler or transpile your code.
Invariant violation: twoslash-vue needs a Vue SFC filename to properly resolve paths and types.
The `createTwoSlashFromVueSFC` function was called without providing a `filename` argument.
fix
Always pass a unique `.vue` filename as the second argument to `createTwoSlashFromVueSFC`, e.g., `createTwoSlashFromVueSFC(code, 'MyComponent.vue')`.
Upgrade
Version history
0.3.8latest on npm
Audit
Dependencies
typescriptrequiredRequired for core TypeScript language services and type inference within Vue SFCs. It's a peer dependency.
Agent activity
5 hits · last 30 days
node
4
Resources
twoslash-vue — npm install twoslash-vue · libregistry