to-type
Fixes JavaScript typeof operator using Symbol.toStringTag
Installation
$ npm i -S @nbsh/to-type
$ yarn add @nbsh/to-type
Usage
import { strictEqual } from 'assert';
import { toType, Types } from '@nbsh/to-type';
toType(4);
toType('abc');
toType(true);
toType({ a: 4 });
toType([1, 2, 3]);
toType(new Date());
toType(/a-z/);
toType(undefined);
toType(null);
toType(new TypeError());
toType(() => {});
strictEqual(toType(4), Types.Number);
strictEqual(toType('abc'), Types.String);
API
toType(object: unknown): string
The toType() method return the default string description of the object it receive as argument. toType() do not affect the object itself.
See Symbol.toStringTag.
Types: enum
The Types enum represent the well-known string descriptions that toType() method return. Note, implementing a class that overrides the toStringTab symbol will not make its value available within this enum.