@loxoz/types-check
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

@loxoz/types-check

A collection of handy functions to quickly check types in your code.

npmGithub

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): boolean

Checks if o is a string

isNum

isNum(o: unknown): boolean

Checks if o is a number

isObj

isObj(o: unknown): boolean

Checks 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): boolean

Checks if o is a Function

isDef

isDef(o: unknown): boolean

Checks 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): boolean

Checks 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): boolean

Checks 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): boolean

Checks if o is an Array (any[], for convenience)

isObjStrict

isObjStrict(o: unknown): boolean

Will 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): boolean

Checks if o is a string with a least one character

isNonEmptyArr

isNonEmptyArr(o: unknown): boolean

Checks 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): boolean

Checks 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): boolean

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

Package Sidebar

Install

npm i @loxoz/types-check

Weekly Downloads

1

Version

1.0.7

License

MIT

Unpacked Size

41 kB

Total Files

11

Last publish

Collaborators

  • loxoz