Vue Prop Types Utility
`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
-
[Vue warn]: Invalid prop: type check failed for prop "myProp". Expected String, got Number.
cause A component received a prop value that did not match the type defined by `vue-types`.fixEnsure the data passed to the prop adheres to the type validation rules defined. Review the prop definition for `myProp` and the data being passed from the parent component. -
Property 'string' does not exist on type 'VueTypesInterface'. Did you mean to call 'VueTypes()'?
cause Attempting to access a type validator as a function call instead of a property, likely a migration issue from an older `vue-types` version.fixAccess type validators directly as properties: `VueTypes.string` instead of `VueTypes().string` (before v6.0.0) or `VueTypes.string()` (which is incorrect for v6+). -
Error: [vue-types] Prop 'myProp' is required.
cause A prop marked with `.isRequired` was not provided to the component.fixPass the required prop from the parent component. If the prop should be optional, remove `.isRequired` from its definition.
Warnings
- breaking In `vue-types` v6.0.0, the `required` and `default` methods no longer return `this`, meaning they cannot be chained like before. They now act as setters.
- breaking With `vue-types` v6.0.0, `VueTypes.oneOf` and `VueTypes.oneOfType` now inherently accept `null` as a valid value unless explicitly marked with `isRequired`.
- breaking The `VueTypes.instanceOf` validator in v6.0.0 no longer accepts an array of constructors for validation.
- breaking As of `vue-types` v6.0.0, the `VueTypes` object itself is no longer directly callable as a factory function. All type validators are accessed as properties.
- gotcha When migrating from `vue-types` v4 to v5+, the default behavior changed for `def()`. Previously, it returned a function, now it returns the value directly. This can impact reactive defaults.
Install
-
npm install vue-types -
yarn add vue-types -
pnpm add vue-types
Imports
- VueTypes
const VueTypes = require('vue-types');import { VueTypes } from 'vue-types'; - PropType
import type { PropType } from 'vue'; - any
VueTypes.any()
VueTypes.any.isRequired
Quickstart
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;