TypeScript Usage
VueTypes is written in TypeScript and comes with full builtin types support.
Most of the validators can infer the prop type from their configuration:
Optional type constraint
Some validators accepts an optional type argument to refine their TS typing.
any<T = any>
string<T = string>
Accepts both strings and enums.
TIP
The same prop type can be expressed using
oneOf
which will also perform a validation at runtime:
number<T = number> and integer<T = number>
func<T = (...args: any[]) => any>
Useful to type event handlers and function return types.
object<T = { [key: string]: any }>
TIP
To have both compile-time and runtime validation, you can use
shape
:
array<T = unknown>
The validator accepts an argument defining the contained items type.
TIP
The same prop types can be expressed using
arrayOf
which will also perform a validation at runtime:
OneOfType<T>
You can use a union type to specify the expected types.
TIP
The same prop types can be expressed composing VueTypes validators:
shape<T>
Setting the type argument provides type checking on top of runtime validation:
custom<T>
You can define the type of the value received by the validator function.
TIP
This validator can be used for tuple props:
oneOf
You can use const assertions on the expected values to constrain the validators type:
Alternative, you can pass a union type:
WARNING
Note that union types don't put any constrain on the presence of all of their members in the validation array. This can lead to runtime bugs not detected by the type checker:
As a general rule, we strongly suggest to use const assertions whenever possible.