Prop type definitions for Vue.js components with runtime validation and TypeScript support
npx @tessl/cli install tessl/npm-vue-types@6.0.0Vue Types provides a comprehensive collection of configurable prop type definitions for Vue.js components, inspired by React's prop-types. It offers type-safe prop validation utilities that help developers define and validate component props with runtime checks and TypeScript support.
npm install vue-typesimport VueTypes from "vue-types";For named imports:
import { string, number, bool, array, object, shape, oneOf } from "vue-types";For CommonJS:
const VueTypes = require("vue-types");
const { string, number, bool } = require("vue-types");For production builds (shim version):
import VueTypes from "vue-types/shim";import VueTypes from "vue-types";
// Define component props with validation
export default {
props: {
// Basic types with sensible defaults
title: VueTypes.string.isRequired,
count: VueTypes.number.def(0),
isActive: VueTypes.bool.def(false),
// Complex validation
items: VueTypes.arrayOf(VueTypes.string).def(() => []),
user: VueTypes.shape({
name: VueTypes.string.isRequired,
age: VueTypes.number,
email: VueTypes.string
}).isRequired,
// Enum-style validation
status: VueTypes.oneOf(['pending', 'success', 'error']).def('pending')
}
};Vue Types is built around several key components:
VueTypes class providing all validators as static properties and methodsBasic prop validators for JavaScript native types with built-in validation and default value support.
// Basic type validators
static get any(): VueTypeValidableDef<any>;
static get func(): VueTypeValidableDef<Function>;
static get bool(): VueTypeValidableDef<boolean>;
static get string(): VueTypeValidableDef<string>;
static get number(): VueTypeValidableDef<number>;
static get array(): VueTypeValidableDef<any[]>;
static get object(): VueTypeValidableDef<Record<string, any>>;
static get integer(): VueTypeDef<number>;
static get symbol(): VueTypeDef<symbol>;
static get nullable(): PropOptions<null>;Advanced validators for custom types, enums, collections, and object structures.
// Custom validation
static custom<T>(validatorFn: ValidatorFunction<T>, warnMsg?: string): VueTypeDef<T>;
// Enum-style validation
static oneOf<T extends readonly any[]>(arr: T): VueTypeDef<T[number]>;
static oneOfType<T extends VueProp<any>[]>(arr: T): VueTypeDef<InferType<T[number]>>;
// Collection validation
static arrayOf<T extends VueProp<any>>(type: T): VueTypeDef<InferType<T>[]>;
static objectOf<T extends VueProp<any>>(type: T): VueTypeDef<Record<string, InferType<T>>>;
// Instance validation
static instanceOf<C extends Constructor>(constructor: C): VueTypeDef<InstanceType<C>>;
// Object structure validation
static shape<T extends object>(obj: ShapeObject<T>): VueTypeShape<T>;Core utilities for creating, extending, and managing prop type definitions.
// Create type definitions
function toType<T>(name: string, obj: PropOptions<T>): VueTypeDef<T>;
function toValidableType<T>(name: string, obj: PropOptions<T>): VueTypeValidableDef<T>;
// Extend existing types
function fromType<T extends VueTypeDef<any>>(
name: string,
source: T,
props?: PropOptions<InferType<T>>
): T;
// Validate values
function validateType<T, U>(type: T, value: U, silent?: boolean): string | boolean;
// Create custom VueTypes classes
function createTypes(defs?: Partial<VueTypesDefaults>): VueTypesInterface;Configuration options and utility functions for customizing behavior and validation.
// Global configuration
interface VueTypesConfig {
silent: boolean;
logLevel: 'log' | 'warn' | 'error' | 'debug' | 'info';
}
const config: VueTypesConfig;
// Utility methods
static utils: {
validate<T, U>(value: T, type: U): boolean;
toType<T>(
name: string,
obj: PropOptions<T>,
validable?: boolean
): VueTypeDef<T> | VueTypeValidableDef<T>;
};// Core type definition interfaces
interface VueTypeDef<T = unknown> extends VueTypeBaseDef<T> {}
interface VueTypeValidableDef<T = unknown> extends VueTypeBaseDef<T> {
readonly validate: (fn: ValidatorFunction<T>) => this & { validator: ValidatorFunction<T> };
}
interface VueTypeBaseDef<T = unknown> extends PropOptions<T> {
_vueTypes_name: string;
readonly def: (def?: DefaultType<T>) => this & { default: DefaultType<T> };
readonly isRequired: this & { required: true };
}
// Shape validator types
interface VueTypeShape<T> extends VueTypeBaseDef<T> {
readonly loose: VueTypeLooseShape<T>;
}
interface VueTypeLooseShape<T> extends VueTypeBaseDef<T> {
readonly loose: VueTypeLooseShape<T>;
readonly _vueTypes_isLoose: true;
}
// Utility types
type ValidatorFunction<T> = (value: T, props?: Record<string, unknown>) => boolean;
type DefaultType<T> = T extends NativeType ? T : DefaultFactory<T>;
type DefaultFactory<T> = (() => T) | T;Vue Types includes comprehensive error handling and warning systems: