Vue Prop Types Utility

6.0.0 · active · verified Wed Apr 22

`vue-types` is a utility library for Vue.js that provides a robust and declarative system for defining and validating component props at runtime, mirroring the functionality of React's `PropTypes`. It offers a comprehensive set of built-in validators for various JavaScript types, complex object shapes, arrays, and allows for custom validation functions. The current stable version is 6.0.0, which targets Vue 3 and Node.js >= 14.0.0. The library maintains a consistent release cadence, with major versions often introducing breaking changes to improve API consistency and align with Vue's evolving ecosystem. Its key differentiator is providing a familiar and highly readable API for prop validation, enhancing component reliability and developer experience by catching prop-related errors early in the development cycle, particularly valuable in large-scale Vue 3 applications for stricter type enforcement and better code maintainability.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a Vue 3 component with various prop types using `vue-types`, including required strings, default values, complex object shapes, arrays, and custom validators.

import { defineComponent } from 'vue';
import { VueTypes } from 'vue-types';

interface User { 
  id: number; 
  name: string; 
  email?: string; 
}

const MyComponent = defineComponent({
  name: 'MyComponent',
  props: {
    message: VueTypes.string.isRequired,
    count: VueTypes.integer.def(0),
    // Define a complex object shape
    user: VueTypes.shape<User>({
      id: VueTypes.integer.isRequired,
      name: VueTypes.string.isRequired,
      email: VueTypes.string.loose,
    }).isRequired,
    // An array of a specific type
    tags: VueTypes.arrayOf(VueTypes.string).loose,
    // A custom validator example
    status: VueTypes.string.validate(value => ['active', 'inactive', 'pending'].includes(value as string)).def('pending')
  },
  setup(props) {
    console.log(`Message: ${props.message}`);
    console.log(`User name: ${props.user.name}`);
    return {};
  },
  template: `
    <div>
      <h1>{{ message }}</h1>
      <p>Count: {{ count }}</p>
      <p>User: {{ user.name }} (ID: {{ user.id }})</p>
      <p v-if="tags && tags.length">Tags: {{ tags.join(', ') }}</p>
      <p>Status: {{ status }}</p>
    </div>
  `
});

export default MyComponent;

view raw JSON →