This library is not supposed to be a schema validator, or a object validator, so it doesn't throw any errors and will always return a boolean result. You should use this library as a quick helper to narrow from more generic types, to something that you can work with.
For example:
Here you're receiving an unknown
object, which you can't work with on typescript.
By the time the program evaluates the 'if' condition as true, constant a
type will be narrow, meaning that now you can be sure that constant a
is a object
and has a prop b
of type number
.
This program will enter inside 'if' condition only if constant a
matches exactly this condition, and your IDE will respect these types.
Under the hood, it uses advanced user defined types to bring the best of two worlds: a fast runtime check within a amazing developer experience.
Checks if target is an array and it has an element on a specific position.
Checks if target is an array and it has an element on a specific position. Can be composed to check the type of the element.
Checks if target is an object and it has a specific property.
Checks if target is an object and it has a specific property. Can be composed to check the type of the property.
Checks if target is an array.
Checks if target is an array. Can be composed to check the type of all elements.
Checks if target is a typescript enum.
Checks if target is a specific enum.
Checks if target is a value of a specific enum.
Checks if target is a number.
Checks if target is an object.
Please notice that null
is a valid javascript object.
Checks if target is one of passed arguments.
Checks if target is all of passed arguments.
Checks if target is a string.
Checks if target is an object and it has all its properties with the same passed type.
Throws if target is null
or undefined
.
Can receive an optional message or anything that extends Error to customize the message to be throw.
Checks if target is a literal with specific value.
Checks if target is a object with specific shape.
Can be composed to check the type of each property individually by using a NarrowFunc
.
It doesn't care about extra properties.
Checks if target is an instance of a class.
Throws if passed condition is not true. Can receive a optional message or anything that extends Error to customize the message to be throw.
Throws if passed condition is not true. It also narrows argument to have matched condition type. Can receive a optional message or anything that extends Error to customize the message to be throw.
Checks if string is one of strings passed in a list.
Checks if target is a boolean.
Throws if target returns false from a NarrowFunc
.
Check if target is not null
, undefined
.