Skip to content

danielbayley/reliable-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reliable Type

Reliable runtime type information.

Install

pnpm install reliable-type

Example

The provided type 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 like this—it can technically be reassigned. It will always return the correct type as an informative, specific Type or tag String. Extra types are also available to import:

import assert from "node:assert/strict"
import { type, types, tag, tags } from "reliable-type"
import { GeneratorFunction } from "reliable-type/types"

function* generator() { yield * [1, 2, 3] }

const Type = type(generator)
const Tag  =  tag(generator)

assert.equal(Type, GeneratorFunction)
assert.equal(Tag, "GeneratorFunction")

assert.equal(Type, types.GeneratorFunction)
assert.equal(Tag,   tags.GeneratorFunction)
Expression type tag
undefined undefined "undefined"
null null "null"
true/false Boolean "Boolean"
"string" String "String"
-0/1_000/0.1 Number "Number"
0xAf/0o1/0b1 Number "Number"
3.1415e+1/Math.PI Number "Number"
2n^63n BigInt "BigInt"
[-]Infinity Infinity "Infinity"
NaN NaN "NaN"
Math Math "Math"
Symbol("*") Symbol "Symbol"
/.*/g/new RegExp(".*") RegExp "RegExp"
new Error("message") Error "Error"
new EvalError("message") EvalError "EvalError"
new RangeError("message") RangeError "RangeError"
new ReferenceError("message") ReferenceError "ReferenceError"
new SyntaxError("message") SyntaxError "SyntaxError"
new TypeError("message") TypeError "TypeError"
new URIError("message") URIError "URIError"
new AggregateError("message") AggregateError "AggregateError"
new Intl.Segmenter() Segmenter "Segmenter"
new Date() Date "Date"
new URL("https://github.com") URL "URL"
fs.createReadStream(buffer) ReadStream "ReadStream"
fs.createWriteStream("/dev/null") WriteStream "WriteStream"
const buffer = Buffer.from("data") Buffer "Buffer"
new ArrayBuffer(1024) ArrayBuffer "ArrayBuffer"
`new Int8Array([-128, 127]) Int8Array "Int8Array"
new Uint8Array([0, 255]) Uint8Array "Uint8Array"
new new Uint8ClampedArray([0, 255]) Uint8ClampedArray "Uint8ClampedArray"
new Int16Array([-32768, 32767]) Int16Array "Int16Array"
new Uint16Array([0, 65535]) Uint16Array "Uint16Array"
new Int32Array([-2147483648, 2147483647]) Int32Array "Int32Array"
new Uint32Array([0, 4294967295]) Uint32Array "Uint32Array"
new Float32Array([-3.4e38, 3.4e38]) Float32Array "Float32Array"
new Float64Array([-1.8e308, 1.8e308]) Float64Array "Float64Array"
new BigInt64Array([-2n^63n, 2n^63n]) BigInt64Array "BigInt64Array"
new BigUint64Array([0n, 2n^63n]) BigUint64Array "BigUint64Array"
[1 ,2 ,3]/Array.from("abc") Array "Array"
new Set([1, 2, 3]) Set "Set"
new WeakSet() WeakSet "WeakSet"
new WeakMap() WeakMap "WeakMap"
const map = new Map().set("a", 1) Map "Map"
map.entries() Iterator "Iterator"
const object = { a: 1, b: 2 } Object "Object"
new Proxy(object, {}) Proxy "Proxy"
Proxy.revocable(object, {}) Proxy "Proxy"
{ [Symbol.toStringTag]: "tag" } "tag" "tag"
JSON JSON "JSON"
function() { return arguments }(1, 2) Arguments "Arguments"
[].reduce/() => {} Function "Function"
async () => Promise.resolve(true) AsyncFunction "AsyncFunction"
Promise.resolve(true) Promise "Promise"
function* generator() { yield * [1, 2] } GeneratorFunction "GeneratorFunction"
generator() Generator "Generator"
async function* gen() { yield * [1, 2] } AsyncGeneratorFunction "AsyncGeneratorFunction"
gen() AsyncGenerator "AsyncGenerator"
new (class Class {})() Class "Class"
new (class Extended extends Class {})() ExtendedClass "ExtendedClass"
fs.opendir(dir) Dir "Dir"
fs.readdir(dir, { withFileTypes: true }) Dirent "Dirent"

Aliases

import { typeOf, toStringTag } from "reliable-type"

License

MIT © Daniel Bayley

Packages

 
 
 

Contributors