ts-narrow
$3
-
Library basics
-
Library methods
-
isString
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:
!
image
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.
Library methods
$3
Checks if target is an array and it has an element on a specific position.
$3
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.
$3
Checks if target is an object and it has a specific property.
$3
Checks if target is an object and it has a specific property.
Can be composed to check the type of the property.
$3
Checks if target is an array.
$3
Checks if target is an array.
Can be composed to check the type of all elements.
$3
Checks if target is a typescript enum.
$3
Checks if target is a specific enum.
$3
Checks if target is a value of a specific enum.
$3
Checks if target is a number.
$3
Checks if target is an object.
Please notice that
null is a valid javascript object.
$3
Checks if target is one of passed arguments.
$3
Checks if target is all of passed arguments.
$3
Checks if target is a string.
$3
Checks if target is an object and it has all its properties with the same passed type.
$3
Throws if target is
null or
undefined.
Can receive an optional message or anything that extends Error to customize the message to be throw.
$3
Checks if target is a literal with specific value.
$3
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.
$3
Checks if target is an instance of a class.
$3
Throws if passed condition is not true.
Can receive a optional message or anything that extends Error to customize the message to be throw.
$3
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.
$3
Checks if string is one of strings passed in a list.
$3
Checks if target is a boolean.
$3
Throws if target returns false from a
NarrowFunc.
$3
Check if target is not
null,
undefined.