# @bemoje/assert-type

> Perform easy type-checks with a simple type assertion.

Latest version **1.0.1** (published 2020-04-30) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @bemoje/assert-type
pnpm add @bemoje/assert-type
yarn add @bemoje/assert-type
bun add @bemoje/assert-type
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-04-30 |
| First published | 2020-04-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 11.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Benjamin M. Jensen |
| Maintainers | bemoje |
| Keywords | perform, easy, type-checks, simple, type, assertion, constructors, expected, type's, accepted, constructor, null, evaluate, asserttype |

## Links

- npm: https://www.npmjs.com/package/@bemoje/assert-type
- Repository: https://github.com/bemoje/bemoje-assert-type
- Homepage: https://github.com/bemoje/bemoje-assert-type#readme
- Issues: https://github.com/bemoje/bemoje-assert-type/issues
- npm.io page: https://npm.io/package/@bemoje/assert-type

## Dependencies (6)

- [@bemoje/is-node](https://npm.io/package/@bemoje/is-node.md) ^1.0.0
- [@bemoje/is-null](https://npm.io/package/@bemoje/is-null.md) ^1.0.0
- [@bemoje/is-type](https://npm.io/package/@bemoje/is-type.md) ^1.0.1
- [@bemoje/is-array](https://npm.io/package/@bemoje/is-array.md) ^1.0.0
- [@bemoje/is-undefined](https://npm.io/package/@bemoje/is-undefined.md) ^3.0.3
- [@bemoje/throw-type-error](https://npm.io/package/@bemoje/throw-type-error.md) ^1.0.0

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-04-30
- 1.0.0 — 2020-04-25

## README

# @bemoje/assert-type

Perform easy type-checks with a simple type assertion.

#### Version

<span><a href="https://npmjs.org/@bemoje/assert-type" title="View this project on NPM"><img src="https://img.shields.io/npm/v/@bemoje/assert-type" alt="NPM version" /></a></span>

#### Travis CI

<span><a href="https://npmjs.org/@bemoje/assert-type" title="View this project on NPM"><img src="https://travis-ci.org/bemoje/bemoje-assert-type.svg?branch=master" alt="dependencies" /></a></span>

#### Dependencies

<span><a href="https://npmjs.org/@bemoje/assert-type" title="View this project on NPM"><img src="https://david-dm.org/bemoje/bemoje-assert-type.svg" alt="dependencies" /></a></span>

#### Stats

<span><a href="https://npmjs.org/@bemoje/assert-type" title="View this project on NPM"><img src="https://img.shields.io/npm/dt/@bemoje/assert-type" alt="NPM downloads" /></a></span>
<span><a href="https://github.com/bemoje/bemoje-assert-type/fork" title="Fork this project"><img src="https://img.shields.io/github/forks/bemoje/bemoje-assert-type" alt="Forks" /></a></span>

#### Donate

<span><a href="https://www.buymeacoffee.com/bemoje" title="Donate to this project using Buy Me A Beer"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg?label=Buy me a beer!" alt="Buy Me A Beer donate button" /></a></span>
<span><a href="https://paypal.me/forstaaloen" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg?label=PayPal" alt="PayPal donate button" /></a></span>

## Installation

```sh
npm install @bemoje/assert-type
npm install --save @bemoje/assert-type
npm install --save-dev @bemoje/assert-type
```

## Usage

```javascript
import assertType from '@bemoje/assert-type'

/**
 * @param {string} str
 * @param {number} num
 * @param {null} nul
 * @param {number|string} strOrNum
 */
function example(str, num, nul, strOrNum) {
	// quite extensive, but now very simple type-checks.
	assertType(String, str)
	assertType(Number, num)
	assertType(null, nul)
	assertType([String, Number], strOrNum)

	// other code
	return 'Types are checked and OK'
}

const STRING = 'a'
const NUMBER = 1
const ARRAY = []

// NOTE: 'undefined' always passes type check

example()
//=> 'Types are checked and OK'

example(STRING, NUMBER, null, STRING)
//=> 'Types are checked and OK'

example(STRING, NUMBER, null, NUMBER)
//=> 'Types are checked and OK'

example(ARRAY, NUMBER, null, STRING)
//=> throws TypeError('Expected String, got Array')

example(STRING, ARRAY, null, STRING)
//=> throws TypeError('Expected Number, got Array')

example(STRING, NUMBER, ARRAY, STRING)
//=> throws TypeError('Expected Null, got Array')

example(STRING, NUMBER, null, ARRAY)
//=> throws TypeError('Expected String, Number, got Array')

```


## Tests
Uses *Jest* to test module functionality. Run tests to get coverage details.

```bash
npm run test
```

## API
### assertType

Perform easy type-checks with a simple type assertion.

##### Parameters

-   `constructors` **([function][3] | null | [Array][4]&lt;([function][3] | null)>)** The expected type's constructor(s). Also accepted as a "constructor" is the null value.

-   `value` **any** The value to evaluate

##### Returns
**void** 

[1]: #asserttype

[2]: #parameters

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

---
_Source: https://npm.io/package/@bemoje/assert-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
