Vue Component Type Helpers

3.2.7 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `ComponentProps`, `ComponentSlots`, `ComponentEmit`, and `ComponentExposed` to extract types from a hypothetical Vue component.

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);

view raw JSON →