TypeLib JS (part 2) - grouped types

ยท 540 words ยท 3 minute read

Cover image for the post that shows how TypeLib JS groups data types in groups, the shown groups are: <code>isNumber</code>, <code>isNumber</code>, <code>isFunction</code>, <code>isTruthy</code>, <code>isNullish</code>, <code>isKinds</code> the last group wrps the different groups in an array.โ€

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

Learn more about Big Integers

.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.

Truthy values

.isFalsy ๐Ÿ”—

Any falsy value, e.g. NaN, null, undefined, "", 0, false.

More on falsy values

.isNullish ๐Ÿ”—

null and undefined. Unlike any other value, they don’t have any accessible property.

null.toString; // Uncaught TypeError: null has no properties

Nullish value

.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.

More about primitive types

.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.

Link to the repo