@loxoz/types-check v1.0.7
@loxoz/types-check
A collection of handy functions to quickly check types in your code.
⨠This library fully supports TypeScript and will infer the checked type to the variable.
š The main goal of this library is to reduce bundle size when checking for types (like more than two or three times) in your web app, but i also like this way of writing code with type checks.
Usage
Checking types for an API Response or json data:
import { isObj, isStr } from "@loxoz/types-check";
let data = null;
fetch("/api/data")
.then(res => res.json())
.then(json => {
if (isObj(json)) data = json;
});
// ... somewhere else ...
if (isStr(data.name)) {
// data.name is now a string
// ...
}Checking arguments of a function that have multiple / optional types:
import { isStr } from "@loxoz/types-check";
function foo(bar?: string) {
if (!isStr(bar)) bar = "default";
// bar is a string
}It also works using CommonJS:
const { isStr } = require("@loxoz/types-check");API
š§Ŗ primitives
isType
isType(o: unknown, t: string): o is JSType[T];Exactly the same as a typeof (typeof o === t) used by other functions of this library to reduce bundle size.
You should check the other functions provided by this library first before using this one.
isStr
isStr(o: unknown): booleanChecks if o is a string
isNum
isNum(o: unknown): booleanChecks if o is a number
isObj
isObj(o: unknown): booleanChecks if o is an object
This will include class instances, if you don't want that, look at isObjStrict
For typescript convenience, the inferred type is a Record
Note: this function won't include functions and null (typeof null === "object" is truthy)
isFunc
isFunc(o: unknown): booleanChecks if o is a Function
isDef
isDef(o: unknown): booleanChecks if o is neither null nor undefined
This function is just here for coding preferences but can actually save some bundle size if you use it a lot in your code.
Doing o != null is exactly the same (since it does that under the hood)
Also see isUndef
isUndef
isUndef(o: unknown): booleanChecks if o is either null or undefined
This function is just here for coding preferences but can actually save some bundle size if you use it a lot in your code.
Doing o == null is exactly the same (since it does that under the hood)
Also see isDef
isDefNull
isDefNull(o: unknown): booleanChecks if o is not undefined (but can be null)
This function is just here for coding preferences but can actually save some bundle size if you use it a lot in your code.
Doing o !== undefined is exactly the same (since it does that under the hood)
Also see isDef
other
There is also isBigInt, isBool and isSymbol, but they aren't used often so omitted from the docs
š¦ objects
isArr
isArr(o: unknown): booleanChecks if o is an Array (any[], for convenience)
isObjStrict
isObjStrict(o: unknown): booleanWill ensure that o is an object created using {}, Object() or Object.create()
Warning: this method is much slower than isObj
This function won't include class instances, if you want that, look at isObj
The inferred type is
Record<string | number | symbol, any>
š§± helpers
isNonEmptyStr
isNonEmptyStr(o: unknown): booleanChecks if o is a string with a least one character
isNonEmptyArr
isNonEmptyArr(o: unknown): booleanChecks if o is an Array (any[], for convenience) with a least one element
isArrTyped
isArrTyped(o: unknown, predicate: (value: unknown, index: number, array: unknown[]) => boolean): booleanChecks if o is an Array and all of its members satisfy the specified predicate to infer the type of that predicate
For e.g. isArrTyped(arr, isStr) will infer the type string[]
ā assertion
assert
assert(o: unknown, predicate: (value: unknown) => boolean, message?: string): booleanWill throw an AssertionError (which extends TypeError), with the passed message (if any) if the predicate failed (returns false)
Advices
If you need more complex type checking, for example on objects or arrays, I recommend you have a look at superstruct.