As I promised in the past article (Click here if you want to read it), I’ll explain how you can use TypeLib JS to verify groups of data. But before, if you haven’t seen it…
Project page: typelib.schemetastic.com
As we learned in the past article, TypeLib makes data verification easy, for example, you can verify values such as NaN
, Infinity
and -Infinity
. e.g.
type(0/0).is; // "nan"
type(1000/0).is; // "infinity"
type(-1000/0).is; // "-infinity"
type(123).is; // "number"
Pretty neat huh? Being able to do that can help you to avoid mistakes that could break your code. Imagine, for example, that you are getting a user input, let’s say, a pointer coords X an Y and then that number is divided… but what if one of those numbers gets 0
? An error like that could break the app.
TypeLib is very specific detecting types. For example, it can distinguish between different types of functions, different types of errors and other data. But what if you don’t need to be that specific? Maybe you just need to detect any numeric value, any function or any error. That’s why TypeLib also includes other properties aside from .is
to help you to verify wider group types, I’ll explain each shortly:
.isNumber
๐
Includes, numbers (bin, octal, decimal, hex), NaN, Infinity and -Infinity.
type(0/0).isNumber; // true
type(1000/0).isNumber; // true
.isNumeric
๐
Includes the same types as the .isNumber
property, but also includes big integers.
type(123n).isNumeric; // true
type(123n).isNumber; // false
.isFunction
๐
Includes, normal functions (arrow included), generator functions and classes.
Classes are a type of function
.isTruthy
๐
Any truthy value, e.g. 123
, "non-empty strings"
, true
.
.isFalsy
๐
Any falsy value, e.g. NaN
, null
, undefined
, ""
, 0
, false
.
.isNullish
๐
null
and undefined
. Unlike any other value, they don’t have any accessible property.
null.toString; // Uncaught TypeError: null has no properties
.isPrimitive
๐
Almost everything in JS is an object, but some data is treated as primitive values, there are 7 types: numbers, strings, bigint, boolean, symbol, undefined and null.
Don’t be misled because typeof
detects null
as an object, This is considered a bug.
.isObject
๐
Anything that is not a primitive is considered an object, from plain objects {}
, arrays []
, types generated from constructors or classes e.g. new Blob()
, etc.
.isError
๐
Any kind of error, SyntaxError, TypeError, Error, ReferenceError, etc.
Learn more about the different types of errors
.kinds
๐
And at last, this property contains an array with all the kinds that a type has based on the above list.
type(123).kinds; // [ "truthy", "numeric", "number", "primitive" ]
type("").kinds; // [ "falsy", "primitive" ]
type(null).kinds; // [ "falsy", "nullish", "primitive" ]
In the next article I’ll cover how TypeLib makes easier debugging and verifying data with it’s methods .isIt()
, .isNot()
and its function typeErrorIf()
.
Support this project ๐
As you can notice, producing this content and this library requires a lot of hours of research and efforts. And this library is offered for free to everyone under the MIT license. But it would help me a lot if you give it a star ๐ on GitHub, and if you like it, you can spread the word ๐ข if you want to.