@sinakhx/real-type
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

@sinakhx/real-type

npm license tests types

get real type of JavaScript's built-in data structures

Why?

JavaScript's typeof operator is a joke.

typeof null       // "object"
typeof {}         // "object"
typeof []         // "object"
typeof new Date() // "object"

in order to overcome such ambiguity, people use the following way to be more specific:

Object.prototype.toString.call(new Date()) // "[object Date]"

which is far more better, but still has got it's own limitations:

Object.prototype.toString.call(NaN) // "[object Number]"
Object.prototype.toString.call(class Foo{}) // "[object Function]"

if you find it silly to treat NaN as number or classes as functions or even using Object.prototype.toString.call to get an array-like string, you can use @sinakhx/real-type instead:

realTypeOf(null)        // "Null"
realTypeOf({})          // "Object"
realTypeOf([])          // "Array"
realTypeOf(new Date())  // "Date"
realTypeOf(NaN)         // "NaN"
realTypeOf(class Foo{}) // "Class"
realTypeOf(async () => {}) // "AsyncFunction"
realTypeOf(new WeakMap()) // "WeakMap"
// ...

Installation

npm install @sinakhx/real-type
# or
yarn add @sinakhx/real-type

Usage

import realTypeOf from '@sinakhx/real-type';
// or if you are using commonjs:
const realTypeOf = require('@sinakhx/real-type/dist/index.cjs').default;

const customType = realTypeOf(new URL('http://example.com'));
console.log(customType); // "URL"

Want More Examples?

see the tests folder for more detailed examples.


Contributing

Please feel free to open an issue or create a pull request to add a new feature or fix a bug. (see contributing for more details)


License

The MIT License (MIT)

© 2022 Sina Khodabandehloo

Readme

Keywords

Package Sidebar

Install

npm i @sinakhx/real-type

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

8.6 kB

Total Files

7

Last publish

Collaborators

  • sinakhx