`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.
npm install vue-typesVerified import paths — ran on the pinned version, not inferred.
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.
Apply `isRequired` or `def()` as the final call in the chain. For example, `VueTypes.string.isRequired` instead of `VueTypes.string().isRequired()` (pre-v6) or chaining further after `def()`.
If `null` should not be accepted for `oneOf` or `oneOfType` definitions, ensure `isRequired` is appended to the prop definition.
To validate against multiple constructors, use `VueTypes.oneOfType` with individual `VueTypes.instanceOf` calls for each constructor, e.g., `VueTypes.oneOfType([VueTypes.instanceOf(ClassA), VueTypes.instanceOf(ClassB)])`.
Instead of `VueTypes().string`, use `VueTypes.string`. Access all validators directly off the `VueTypes` object.
For reactive default values, wrap the default in a function: `VueTypes.array.def(() => [])`.
Ensure 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.
Access type validators directly as properties: `VueTypes.string` instead of `VueTypes().string` (before v6.0.0) or `VueTypes.string()` (which is incorrect for v6+).
Pass the required prop from the parent component. If the prop should be optional, remove `.isRequired` from its definition.