npm

@nbsh/to-type
TypeScript icon, indicating that this package has built-in type declarations

5.0.0-next.1 • Public • Published

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); // "number"
toType('abc'); // "string"
toType(true); // "boolean"
toType({ a: 4 }); // "object"
toType([1, 2, 3]); // "array"
toType(new Date()); // "date"
toType(/a-z/); // "regexp"
toType(undefined); // "undefined"
toType(null); // "null"
toType(new TypeError()); // "error"
toType(() => {}); // "function"

strictEqual(toType(4), Types.Number); // true
strictEqual(toType('abc'), Types.String); // true

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @nbsh/to-type

Weekly Downloads

1

Version

5.0.0-next.1

License

MIT

Unpacked Size

5.78 kB

Total Files

6

Last publish

Collaborators

  • rannn505