npm package: @loxoz/types-check
Description: A collection of handy functions to quickly check types in your code
Author: Loxoz
Latest version: 1.0.7

# @loxoz/types-check

A collection of handy functions to quickly check types in your code.

[**npm**](https://www.npmjs.com/package/@loxoz/types-check) • [**Github**](https://github.com/Loxoz/types-check)

✨ 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:

```js
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:
```ts
import { isStr } from "@loxoz/types-check";

function foo(bar?: string) {
  if (!isStr(bar)) bar = "default";
  // bar is a string
}
```

It also works using CommonJS:
```js
const { isStr } = require("@loxoz/types-check");
```

## API

## 🧪 primitives

### isType

```ts
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

```ts
isStr(o: unknown): boolean
```

Checks if `o` is a `string`

### isNum

```ts
isNum(o: unknown): boolean
```

Checks if `o` is a `number`

### isObj

```ts
isObj(o: unknown): boolean
```

Checks if `o` is an `object`

This will include class instances, if you don't want that, look at [isObjStrict](#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

```ts
isFunc(o: unknown): boolean
```

Checks if `o` is a `Function`

### isDef

```ts
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

```ts
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](#isdef)

### isDefNull

```ts
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](#isdef)

### other

There is also `isBigInt`, `isBool` and `isSymbol`, but they aren't used often so omitted from the docs

## 📦 objects

### isArr

```ts
isArr(o: unknown): boolean
```

Checks if `o` is an `Array` (`any[]`, for convenience)

### isObjStrict

```ts
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](#isobj)

This function won't include class instances, if you want that, look at [isObj](#isobj)

> The inferred type is `Record<string | number | symbol, any>`

## 🧱 helpers

### isNonEmptyStr

```ts
isNonEmptyStr(o: unknown): boolean
```

Checks if `o` is a `string` with a least one character

### isNonEmptyArr

```ts
isNonEmptyArr(o: unknown): boolean
```

Checks if `o` is an `Array` (`any[]`, for convenience) with a least one element

### isArrTyped

```ts
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

```ts
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](https://www.npmjs.com/package/superstruct).