Vue Component Type Helpers
vue-component-type-helpers is a utility library providing TypeScript type helpers to accurately extract various type definitions from Vue components. It enables developers to statically analyze and derive types for props, slots, attributes, emitted events, and exposed instance properties (via `defineExpose`) from `.vue` files. As of version 3.2.7, it is actively maintained as part of the official Vue.js Language Tools monorepo, receiving frequent updates, typically alongside new Vue Language Server or Volar releases. This library is entirely type-based, ships with its own TypeScript definitions, and has no runtime dependencies, making it a zero-overhead addition to any TypeScript-enabled Vue project. Its primary differentiation lies in providing robust, officially supported type inference for complex Vue component structures, crucial for strong type-checking and IDE support.
Common errors
-
Error: Cannot find module 'vue-component-type-helpers'
cause The package was installed, but its build artifacts were missing in version 3.2.1.fixUpdate `vue-component-type-helpers` to version `3.2.2` or higher: `npm install vue-component-type-helpers@latest`. -
ESLint: 'ComponentProps' is declared but its value is never read. (no-unused-vars)
cause Using a regular `import` statement for a type-only symbol, which is then removed during transpilation, causing linters to flag it as unused.fixChange `import { ComponentProps } from 'vue-component-type-helpers';` to `import type { ComponentProps } from 'vue-component-type-helpers';`.
Warnings
- breaking Version 3.2.1 of `vue-component-type-helpers` was released with missing build files, preventing proper installation and usage.
- gotcha As a TypeScript-only utility library, all imports from `vue-component-type-helpers` should use the `import type` syntax.
- gotcha This library is part of the `vuejs/language-tools` monorepo. For optimal performance and correct type inference, ensure `vue-component-type-helpers` is kept in sync with the versions of `vue-tsc` and your VS Code Volar extension.
Install
-
npm install vue-component-type-helpers -
yarn add vue-component-type-helpers -
pnpm add vue-component-type-helpers
Imports
- ComponentProps
import { ComponentProps } => from 'vue-component-type-helpers';import type { ComponentProps } from 'vue-component-type-helpers'; - ComponentSlots
const { ComponentSlots } = require('vue-component-type-helpers');import type { ComponentSlots } from 'vue-component-type-helpers'; - ComponentExposed
import type { ComponentExposed } from 'vue-component-type-helpers';
Quickstart
import type { ComponentProps, ComponentSlots, ComponentEmit, ComponentExposed } from 'vue-component-type-helpers';
// MyComponent.vue (imaginary file for type extraction)
// <script setup lang="ts">
// defineProps<{
// title: string;
// count?: number;
// }>();
// defineEmits<{
// update: [value: string];
// close: [];
// }>();
// defineSlots<{
// default(props: { item: string }): any;
// header(): any;
// }>();
// const internalState = ref(0);
// defineExpose({
// reset: () => { internalState.value = 0; },
// });
// </script>
// Imagine 'MyComponent' is a Vue component imported from a .vue file
declare const MyComponent: {
__VLS_TypeProps: { title: string; count?: number };
__VLS_TypeSlots: { default(props: { item: string }): any; header(): any };
__VLS_TypeEmits: { (e: 'update', value: string): void; (e: 'close'): void };
__VLS_TypeExposed: { reset: () => void };
// Simplified representation for type checking demonstration
};
type Props = ComponentProps<typeof MyComponent>;
// Expected: { title: string; count?: number }
type Slots = ComponentSlots<typeof MyComponent>;
// Expected: { default(props: { item: string }): any; header(): any }
type Emit = ComponentEmit<typeof MyComponent>;
// Expected: { (e: 'update', value: string): void; (e: 'close'): void }
type Exposed = ComponentExposed<typeof MyComponent>;
// Expected: { reset: () => void }
console.log('Props type example:', {} as Props);
console.log('Slots type example:', {} as Slots);
console.log('Emit type example:', {} as Emit);
console.log('Exposed type example:', {} as Exposed);