# @wareset/typed

> Checking and comparing with the prototype

Latest version **0.2.5** (published 2020-10-04) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install @wareset/typed
pnpm add @wareset/typed
yarn add @wareset/typed
bun add @wareset/typed
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2020-10-04 |
| First published | 2020-06-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | cftcview |
| Maintainers | nk12 |
| Keywords | type, typeof, instanceof, proto, prototype |

## Links

- npm: https://www.npmjs.com/package/@wareset/typed
- Repository: https://github.com/wareset/wareset
- Homepage: https://github.com/wareset/wareset/tree/master/packages/typed#readme
- Issues: https://github.com/wareset/wareset/issues
- npm.io page: https://npm.io/package/@wareset/typed

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.2.5 (latest) — 2020-10-04
- 0.2.4 — 2020-09-29
- 0.2.3 — 2020-09-27
- 0.2.2 — 2020-09-27
- 0.2.1 — 2020-09-26
- 0.2.0 — 2020-09-25
- 0.1.1 — 2020-08-01
- 0.1.0 — 2020-08-01
- 0.0.9 — 2020-08-01
- 0.0.8 — 2020-08-01
- 0.0.7 — 2020-07-17
- 0.0.6 — 2020-06-28
- 0.0.5 — 2020-06-28
- 0.0.4 — 2020-06-28
- 0.0.3 — 2020-06-26
- … 1 more at https://npm.io/package/@wareset/typed/versions

## README

# `@wareset/typed`

He can return the constructor, return the entire chain of constructors, check the availability of constructors.

### Attention:

##### <=0.1.1

Version <=0.1.1 returns the prototypes and not the constructors!

##### >=0.2.3

Starting with the >=0.2.3 version, the method `typed.of` returns a `NULL`.
Technically, this is more correct, because `NULL` is also an object, but without a constructor.

## Installation

```bash
npm i @wareset/typed ## yarn add @wareset/typed
```

## Usage

### Import:

```js
import typed from '@wareset/typed';
```

### Method: `typed(value: any)`

Returns prototype:

```js
assert(typed(null), null);
assert(typed(undefined), undefined);

assert(typed('str'), String);
assert(typed(30401), Number);
assert(typed(false), Boolean);
assert(typed({ q: 1 }), Object);
// etc...

assert(
  typed(() => {}),
  Function
);
assert(
  typed(async () => {}),
  AsyncFunction
);

const DIV = document.createElement('div');
assert(typed(DIV), HTMLDivElement);
// etc...
```

### Method: `typed.of(value: any)`

Returns an array of all prototypes:

```js
assert(typed.of(null), [null]);
assert(typed.of(undefined), [undefined]);

assert(typed.of({ q: 1 }), [Object, null]);
assert(typed.of('string'), [String, Object, null]);
// etc...

assert(
  typed.of(() => {}),
  [Function, Object, null]
);
assert(
  typed.of(async () => {}),
  [AsyncFunction, Function, Object, null]
);

const H1 = document.createElement('h1');
console.log(typed.of(H1));
/* RETURN: */ [
  HTMLHeadingElement,
  HTMLElement,
  Element,
  Node,
  EventTarget,
  Object,
  null
];
// etc...
```

### Methods: `typed(value: any, ...types)` and `typed.of(value: any, ...types)`

Check the availability of prototypes:

```js
// typed
console.log(typed(9999, [Boolean, String, Element, {}, Object]));
/* RETURN: */ false; // because they are not a Number

console.log(typed(9999, Number));
console.log(typed(9999, [Number]));
console.log(typed(9999, 1111));
console.log(typed(9999, [Number, String])); // because there is a Number
/* RETURN: */ true;

// typed.of
console.log(typed.of(9999, [Boolean, String, Node]));
/* RETURN: */ false; // because they are not a Number or Object

console.log(typed.of(9999, Number));
console.log(typed.of(9999, [Object])); // because Object
console.log(typed.of(9999, [1111]));
/* RETURN: */ true;

// other
const someFn = () => {};
const someFnAsync = async () => {};

console.log(typed(someFn, Function)); // true
console.log(typed(someFn, () => {})); // true
console.log(typed.of(someFnAsync, someFn)); // true
console.log(typed.of(someFn, someFnAsync)); // false
console.log(typed(someFnAsync, someFn)); // false
console.log(typed(someFn, someFnAsync)); // false
// etc...
```

### Methods: `typed.check(value: any, ...types)` and `typed.of.check(value: any, ...types)`

Check the availability of prototypes:

```js
console.log(typed.check(9999)); // RETURN: 9999
console.log(typed.of.check(9999)); // RETURN: 9999

console.log(typed.check(9999, Number)); // RETURN: 9999
console.log(typed.of.check(9999, [String, Object])); // RETURN: 9999

console.log(typed.check(9999, String)); // throw new Error()
console.log(typed.of.check(9999, [String, Node])); // throw new Error()

// LIVE EXAMPLE:

function strictTypedFn(element, options = {}) {
  typed.of.check(element, Element), typed.check(options, Object);

  // some code ...
}

// This function will succeed:
try {
  strictTypedFn(document.createElement('div'), { param1: 1, param2: 2 });
} catch (err) {
  // ...
}

// This function will return an exception:
try {
  strictTypedFn(12, 'string');
} catch (err) {
  // ...
}
```

## License

MIT

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