Reliable Type
Reliable runtime type information.
[p]npm install reliable-type --save
The provided typeOf
method is more reliable than some combination of
typeof
and instanceof
… While the .constructor
Object
property is
strong—and recommended in absence of a dependency, or custom function
—it can
technically be reassigned. This typeOf
method will always return
the correct
type as an informative, specific String
:
import {typeOf} from "reliable-type"
typeOf(function* generator() { yield * [1, 2, 3] }) // "GeneratorFunction"
Expression | Type |
---|---|
null |
"null" |
undefined |
"undefined" |
true /false |
"Boolean" |
"string" |
"String" |
-0 /1_000 /0.1 |
"Number" |
0xAf /0o1 /0b1 |
"Number" |
3.1415e+1 /Math.PI |
"Number" |
1_000_000n |
"BigInt" |
[- ]Infinity |
"Infinity" |
NaN |
"NaN" |
Math |
"Math" |
Symbol("*") |
"Symbol" |
new Intl.Segmenter("en-US") |
"Segmenter" |
new Date() |
"Date" |
/.*/g /new RegExp(".*") |
"RegExp" |
new URL("https://github.com") |
"URL" |
Buffer.from("data") |
"Buffer" |
[1 ,2 ,3] /Array.from("abc") |
"Array" |
new Uint8Array([0, 255]) |
"Uint8Array" |
new Set([1, 2, 3]) |
"Set" |
new WeakSet() |
"WeakSet" |
new WeakMap() |
"WeakMap" |
new Map().set("a", 1) |
"Map" |
{ a: 1, b: 2 } |
"Object" |
{ [Symbol.toStringTag]: "tag" } |
"tag" |
JSON |
"JSON" |
function() { return arguments }(1, 2) |
"Arguments" |
[].reduce /() => {} |
"Function" |
async () => Promise.resolve(true) |
"AsyncFunction" |
Promise.resolve(true) |
"Promise" |
function* generator() { yield * [1, 2] } |
"GeneratorFunction" |
generator() |
"Generator" |
async function* gen() { yield * [1, 2] } |
"AsyncGeneratorFunction" |
gen() |
"AsyncGenerator" |
new (class Class {})() |
"Class" |
new (class Extended extends Class {})() |
"Extended" |