typeinfo is a lightweight JavaScript library that extends the functionality
of the typeof
operator, allowing for getting more precise and reliable type
information of JavaScript variables. It provides enhanced support for the latest
ECMAScript standards and offers a comprehensive solution for type identification
in your projects.
- Accurate type information for JavaScript variables.
- Support for the latest ECMAScript standards.
- Easy integration into your projects.
You can install typeinfo via npm
:
npm install @haixing_hu/typeinfo
or via yarn
:
yarn add @haixing_hu/typeinfo
The following is a usage example:
import typeInfo from '@haixing_hu/typeinfo';
function clone(value) {
const info = typeInfo(value);
switch (info.type) {
case 'undefined': // drop down
case 'null': // drop down
case 'boolean': // drop down
case 'number': // drop down
case 'string': // drop down
case 'symbol': // drop down
case 'bigint': // drop down
case 'function': // drop down
return value; // don't need to clone immutable objects
case 'object': // drop down
default:
switch (info.subtype) {
case 'Boolean': // drop down
case 'Number': // drop down
case 'String': // drop down
return value; // don't need to clone immutable objects
case 'Date':
return new Date(value);
case 'RegExp':
return new RegExp(value);
...
}
}
}
or use info.category to simplify the code logic:
import typeInfo from '@haixing_hu/typeinfo';
function clone(value) {
const info = typeInfo(value);
switch (info.category) {
case 'undefined': // drop down
case 'null': // drop down
case 'primitive': // drop down
case 'function':
return value; // don't need to clone immutable objects
case 'primitive-wrapper':
return value; // don't need to clone immutable objects
case 'date':
return new Date(value);
case 'regexp':
return new RegExp(value);
...
}
}
The library provides the following function:
function typeInfo(value)
- Parameters:
-
value: any
: a specified value.
-
- Returns:
-
object
: the information about the type of the specified value.
-
This function returns the information about the precise type of the specified value. The returned information is an object with the following properties:
-
type: string
: the name of the type of the specified value. This is the same as the value returned by the built-intypeof
operator, except that the type ofnull
is'null'
instead of'object'
. See Type for more details. -
subtype: string
: the name of the subtype of the specified value. This property is only presented when the type of the specified value is'object'
or'function'
. See Subtype for more details. -
category: string
: the category of ths specified value. This property is present for all type of values. See Category for more details. -
isPrimitive: boolean
: whether the specified value is a primitive value. -
isBuiltIn: boolean
: whether the specified value is a JavaScript built-in primitive or built-in object. -
isWebApi: boolean
: whether the specified value is a Web API built-in object. -
constructor: function
: the constructor function of the specified value. This property is only present when the type of the specified value is'object'
.
The type information object returned by typeInfo()
has a type
property,
which has the following possible values:
-
'undefined'
: if the value isundefined
. -
'null'
: if the value isnull
. -
'boolean'
: if the value is a primitive boolean value. -
'number'
: if the value is a primitive number value. -
'string'
: if the value is a primitive string value. -
'symbol'
: if the value is a symbol value. -
'bigint'
: if the value is a primitive bigint value. -
'function'
: if the value is a function. -
'object'
: if the value is an object.
The type
property value is similar to the value returned by the built-in
typeof
operator, except that the type of null
is 'null'
instead of
'object'
.
If the value is of the type
is function
or object
, the type information
object returned by typeInfo()
has a subtype
property, which is the name of
the detailed subtype of the specified value.
The possible subtype
names of the 'function'
type are:
-
'Function'
: if the value is a sync function. -
'GeneratorFunction'
: if the value is a sync generator function. -
'AsyncFunction'
: if the value is an async function. -
'AsyncGeneratorFunction'
: if the value is an async generator function.
Note that the 'AsyncFunction'
and 'AsyncGeneratorFunction'
subtypes are
only available in the JavaScript engine that support the async functions.
The possible subtype
names of the 'object'
type are:
-
'Boolean'
: if the value is a JavaScript built-inBoolean
object. -
'Number'
: if the value is a JavaScript built-inNumber
object. -
'String'
: if the value is a JavaScript built-inString
object. -
'RegExp'
: if the value is a regular expression, i.e., the JavaScript built-inRegExp
object. -
'Date'
: if the value is a JavaScript built-inDate
object. -
'Map'
: if the value is a JavaScript built-inMap
object. -
'WeakMap'
: if the value is a JavaScript built-inWeakMap
object. -
'Set'
: if the value is a JavaScript built-inSet
object. -
'WeakSet'
: if the value is a JavaScript built-inWeakSet
object. -
'Array'
: if the value is a JavaScript built-inArray
object. -
'Int8Array'
: if the value is a JavaScript built-inInt8Array
object. -
'Uint8Array'
: if the value is a JavaScript built-inUint8Array
object. -
'Uint8ClampedArray'
: if the value is a JavaScript built-inUint8ClampedArray
object. -
'Int16Array'
: if the value is a JavaScript built-inInt16Array
object. -
'Uint16Array'
: if the value is a JavaScript built-inUint16Array
object. -
'Int32Array'
: if the value is a JavaScript built-inInt32Array
object. -
'Uint32Array'
: if the value is a JavaScript built-inUint32Array
object. -
'BigInt64Array'
: if the value is a JavaScript built-inBigInt64Array
object. -
'BigUint64Array'
: if the value is a JavaScript built-inBigUint64Array
object. -
'Float32Array'
: if the value is a JavaScript built-inFloat32Array
object. -
'Float64Array'
: if the value is a JavaScript built-inFloat64Array
object. -
'ArrayBuffer'
: if the value is a JavaScript built-inArrayBuffer
object. -
'SharedArrayBuffer'
: if the value is a JavaScript built-inSharedArrayBuffer
object. -
'DataView'
: if the value is a JavaScript built-inDataView
object. -
'WeakRef'
: if the value is a JavaScript built-inWeakRef
object. -
'Promise'
: if the value is a JavaScript built-inPromise
object. -
'Error'
: if the value is an object of the JavaScript built-inError
class. -
'EvalError'
: if the value is an object of the JavaScript built-inEvalError
class. -
'RangeError'
: if the value is an object of the JavaScript built-inRangeError
class. -
'ReferenceError'
: if the value is an object of the JavaScript built-inReferenceError
class. -
'SyntaxError'
: if the value is an object of the JavaScript built-inSyntaxError
class. -
'TypeError'
: if the value is an object of the JavaScript built-inTypeError
class. -
'URIError'
: if the value is an object of the JavaScript built-inURIError
class. -
'AggregateError'
: if the value is an object of the JavaScript built-inAggregateError
class. -
'InternalError'
: if the value is an object of the JavaScript built-inInternalError
class. -
'Intl.Collator'
: if the value is a JavaScript built-inIntl.Collator
object. -
'Intl.DateTimeFormat'
: if the value is a JavaScript built-inIntl.DateTimeFormat
object. -
'Intl.DisplayNames'
: if the value is a JavaScript built-inIntl.DisplayNames
object. -
'Intl.DurationFormat'
: if the value is a JavaScript built-inIntl.DurationFormat
object. -
'Intl.ListFormat'
: if the value is a JavaScript built-inIntl.ListFormat
object. -
'Intl.Locale'
: if the value is a JavaScript built-inIntl.Locale
object. -
'Intl.NumberFormat'
: if the value is a JavaScript built-inIntl.NumberFormat
object. -
'Intl.PluralRules'
: if the value is a JavaScript built-inIntl.PluralRules
object. -
'Intl.RelativeTimeFormat'
: if the value is a JavaScript built-inIntl.RelativeTimeFormat
object. -
'Intl.Segmenter'
: if the value is a JavaScript built-inIntl.Segmenter
object. -
'MapIterator'
: if the value is aMap
Iterator returned by-
Map.prototype.values()
, -
Map.prototype.keys()
, -
Map.prototype.entries()
, and -
Map.prototype[@@iterator]()
.
-
-
'SetIterator'
: if the value is aSet
Iterator returned by-
Set.prototype.values()
, -
Set.prototype.keys()
, -
Set.prototype.entries()
, and -
Set.prototype[@@iterator]()
.
-
-
'ArrayIterator'
: if the value is aArray
iterator returned by-
Array.prototype.values()
, -
Array.prototype.keys()
, -
Array.prototype.entries()
, -
Array.prototype[@@iterator]()
, -
TypedArray.prototype.values()
, -
TypedArray.prototype.keys()
, and -
TypedArray.prototype.entries()
.
-
-
'StringIterator'
: if the value is aString
Iterator returned by-
String.prototype[@@iterator]()
.
-
-
'RegExpStringIterator'
: if the value is a RegExp String Iterator returned by-
RegExp.prototype[@@matchAll]()
, and -
String.prototype.matchAll()
.
-
-
'SegmenterStringIterator'
: if the value is a Segments Iterator returned by- the
[@@iterator]()
method of the Segments object returned byIntl.Segmenter.prototype.segment()
.
- the
-
'FinalizationRegistry'
: if the value is an instance of the JavaScript built-inFinalizationRegistry
class. AFinalizationRegistry
object lets you request a callback when a value is garbage-collected. -
'Arguments
: if the value is the JavaScript built-inarguments
object, which is a special array-like object storing the calling arguments of a function. -
'Generator'
: if the value is a generator object, i.e., the object returned by a sync generator function. -
'AsyncGenerator'
: if the value is an async generator object, i.e., the object returned by an async generator function. -
'GlobalObject'
: if the value is the global object. A global object is an object that always exists in the global scope. -
'Object'
: if the value is a plain JavaScript object, i.e., an object defined by the syntax ofobj = { ... }
. -
''
(empty string): if the value is an instance of a user defined anonymous class. -
value[Symbol.toStringTag]
: if the value has a customizedSymbol.toStringTag
property. -
value.constructor.name
: if the value has a constructor with a name, and the name is not'Object'
. That is, if the value is an instance of a user defined class, and the class has a name, thesubtype
is the name of that class. For example, if the value is an instance of theMyClass
class, thesubtype
is'MyClass'
. - the name extracted from
value.toString()
: if the value does not match any of the above cases, thesubtype
is the name extracted from thevalue.toString()
result (usually a string of the form'[object XXX]'
), and removes any inner spaces in the name. For example, if thevalue.toString()
result is'[object My Class ]'
, thesubtype
is'MyClass'
.
The detailed list of supported JavaScript built-in objects can be found at Standard built-in objects.
The type information object returned by typeInfo()
has a category
property,
which is a string that describes the category of the value. The possible values
of the category
property are:
-
'null'
: if the value isnull
. -
'undefined'
: if the value isundefined
. -
'boolean'
: if the value is a primitiveboolean
value or a built-inBoolean
object. -
'numeric'
: if the value is a primitivenumber
value, or a primitivebigint
value, or a built-inNumber
object. -
'string'
: if the value is a primitivestring
value, or a built-inString
object. -
'symbol'
: if the value is a primitivesymbol
value. -
'function'
: if the value is a function, including sync functions, async functions, sync generator functions, and async generator functions. -
'regexp'
: if the value is a regular expression, i.e., the JavaScript built-inRegExp
object. -
'date'
: if the value is a JavaScript built-inDate
object. -
'map'
: if the value is a JavaScript built-inMap
object. -
'set'
: if the value is a JavaScript built-inSet
object. -
'array'
: if the value is a JavaScript built-inArray
object. -
'typed-array'
: if the value is a JavaScript built-in typed array object, including'Int8Array'
,'Uint8Array'
,'Uint8ClampedArray'
,'Int16Array'
,'Uint16Array'
,'Int32Array'
,'Uint32Array'
,'BigInt64Array'
,'BigUint64Array'
,'Float32Array'
, and'Float64Array'
. -
'buffer'
: if the value is a JavaScript built-in buffer object, including'ArrayBuffer'
and'SharedArrayBuffer'
. -
'data-view'
: if the value is a JavaScript built-inDataView
object. -
'weak'
: if the value is a JavaScript built-inWeakMap
,WeakSet
, orWeakRef
object. Note that weak referenced objects cannot be cloned. -
'promise'
: if the value is a JavaScript built-inPromise
object. -
'error'
: if the value is an object of the JavaScript built-inError
class, or an object of a subclass of theError
class. -
'intl'
: if the value is a JavaScript built-in object under theIntl
namespace, including'Intl.Collator'
,'Intl.DateTimeFormat'
,'Intl.DisplayNames'
,'Intl.DurationFormat'
,'Intl.ListFormat'
,'Intl.Locale'
,'Intl.NumberFormat'
,'Intl.PluralRules'
, and'Intl.RelativeTimeFormat'
. -
'iterator'
: if the value is an iterator object, including'MapIterator'
,'SetIterator'
,'ArrayIterator'
,'StringIterator'
,'RegExpStringIterator'
, and'SegmenterStringIterator'
. -
'finalization-registry'
: if the value is an instance of the JavaScript built-inFinalizationRegistry
class. AFinalizationRegistry
object lets you request a callback when a value is garbage-collected. -
'global'
: if the value is the global object. -
'arguments'
: if the value is the JavaScript built-inarguments
object. -
'DOM'
: if the value is a DOM object, e.g., the'Node'
,'Element'
,'Document'
,'Window'
, etc. -
'CSSOM'
: if the value is a CSSOM object, e.g.,'CSSStyleDeclaration'
,'CSSRule'
,'CSSStyleSheet'
, etc. -
'event'
: if the value is a event object, i.e., the'Event'
and all its subclasses. -
'console'
: if the value is the'window.console'
object. -
'file'
: if the value is a File API object, i.e., the'File'
,'Blob'
,'FileList'
, etc. -
'generator'
: if the value is a generator object, i.e., the object returned by a sync generator function, including'Generator'
and'AsyncGenerator'
. -
'object'
: if the value is a plain JavaScript object. -
'class'
: if the value is an instance of a user defined class.
This library provides the following constants for feature detection:
-
AGGREGATEERROR_EXISTS
: whether the JavaScript built-in classAggregateError
exists. -
ARRAYBUFFER_EXISTS
: whether the JavaScript built-in classArrayBuffer
exists. -
ARRAY_ISARRAY_EXISTS
: whether the JavaScript built-in functionArray.isArray()
exists. -
ARRAY_ITERATOR_EXISTS
: whether the JavaScript built-in functionArray.prototype[Symbol.iterator]
exists. -
ATOMICS_EXISTS
: whether the JavaScript built-in objectAtomics
exists. -
BIGINT64ARRAY_EXISTS
: whether the JavaScript built-in classBigInt64Array
exists. -
BIGINT_EXISTS
: whether the JavaScript built-in primitivebigint
and built-in functionBigInt
exists. -
BIGUINT64ARRAY_EXISTS
: whether the JavaScript built-in classBigUint64Array
exists. -
DATAVIEW_EXISTS
: whether the JavaScript built-in classDataView
exists. -
FINALIZATIONREGISTRY_EXISTS
: whether the JavaScript built-in classFinalizationRegistry
exists. -
FLOAT32ARRAY_EXISTS
: whether the JavaScript built-in classFloat32Array
exists. -
FLOAT64ARRAY_EXISTS
: whether the JavaScript built-in classFloat64Array
exists. -
INT16ARRAY_EXISTS
: whether the JavaScript built-in classInt16Array
exists. -
INT32ARRAY_EXISTS
: whether the JavaScript built-in classInt32Array
exists. -
INT8ARRAY_EXISTS
: whether the JavaScript built-in classInt8Array
exists. -
INTERNALERROR_EXISTS
: whether the JavaScript built-in classInternalError
class exists. -
INTL_COLLATOR_EXISTS
: whether the JavaScript built-in classIntl.Collator
class exists. -
INTL_DATETIMEFORMAT_EXISTS
: whether the JavaScript built-in classIntl.DateTimeFormat
class exists. -
INTL_DISPLAYNAMES_EXISTS
: whether the JavaScript built-in classIntl.DisplayNames
class exists. -
INTL_DURATIONFORMAT_EXISTS
: whether the JavaScript built-in classIntl.DurationFormat
class exists. -
INTL_EXISTS
: whether the JavaScript built-inIntl
namespace exists. -
INTL_LISTFORMAT_EXISTS
: whether the JavaScript built-in classIntl.ListFormat
exists. -
INTL_LOCALE_EXISTS
: whether the JavaScript built-in classIntl.Locale
exists. -
INTL_NUMBERFORMAT_EXISTS
: whether the JavaScript built-in classIntl.NumberFormat
exists. -
INTL_PLURALRULES_EXISTS
: whether the JavaScript built-in classIntl.PluralRules
exists. -
INTL_RELATIVETIMEFORMAT_EXISTS
: whether the JavaScript built-in classIntl.RelativeTimeFormat
exists. -
INTL_SEGMENTER_EXISTS
: whether the JavaScript built-in classIntl.Segmenter
exists. -
INTL_SEGMENTER_ITERATOR_EXISTS
: whether the JavaScript built-in functionIntl.Segmenter.prototype[Symbol.iterator]
exists. -
MAP_ENTRIES_EXISTS
: whether the JavaScript built-in functionMap.prototype.entries()
exists. -
MAP_EXISTS
: whether the JavaScript built-in classMap
exists. -
MAP_ITERATOR_EXISTS
: whether the JavaScript built-in functionMap.prototype[Symbol.iterator]
exists. -
PROMISE_EXISTS
: whether the JavaScript built-in classPromise
exists. -
PROXY_EXISTS
: whether the JavaScript built-in classProxy
exists. -
REFLECT_EXISTS
: whether the JavaScript built-in namespaceReflect
exists. -
REGEXP_EXISTS
: whether the JavaScript built-in classRegExp
exists. -
REGEXP_ITERATOR_EXISTS
: whether the JavaScript built-in functionRegExp.prototype[Symbol.matchAll]
exists. -
SET_ENTRIES_EXISTS
: whether the JavaScript built-in functionSet.prototype.entries()
exists. -
SET_EXISTS
: whether the JavaScript built-in classSet
exists. -
SET_ITERATOR_EXISTS
: whether the JavaScript built-in functionSet.prototype[Symbol.iterator]
exists. -
SHAREDARRAYBUFFER_EXISTS
: whether the JavaScript built-in classSharedArrayBuffer
exists. -
STRING_ITERATOR_EXISTS
: whether the JavaScript built-in functionString.prototype[Symbol.iterator]
exists. -
SYMBOL_EXISTS
: whether the JavaScript built-inSymbol
exists. -
SYMBOL_ITERATOR_EXISTS
: whether the JavaScript built-in functionSymbol.prototype[Symbol.iterator]
exists. -
SYMBOL_MATCH_ALL_EXISTS
: whether the JavaScript built-in functionSymbol.prototype[Symbol.matchAll]
exists. -
SYMBOL_TO_STRING_TAG_EXISTS
: whether the JavaScript built-in functionSymbol.prototype[Symbol.toStringTag]
exists. -
UINT16ARRAY_EXISTS
: whether the JavaScript built-in classUint16Array
exists. -
UINT32ARRAY_EXISTS
: whether the JavaScript built-in classUint32Array
exists. -
UINT8ARRAY_EXISTS
: whether the JavaScript built-in classUint8Array
exists. -
UINT8CLAMPEDARRAY_EXISTS
: whether the JavaScript built-in classUint8ClampedArray
exists. -
WEAKMAP_EXISTS
: whether the JavaScript built-in classWeakMap
exists. -
WEAKREF_EXISTS
: whether the JavaScript built-in classWeakRef
exists. -
WEAKSET_EXISTS
: whether the JavaScript built-in classWeakSet
exists.
The following code shows how to use these constants:
import { WEAKMAP_EXISTS } from '@haixing_hu/typeinfo';
function foo(value) {
if (WEAKMAP_EXISTS) {
...
} else {
...
}
}
This library provides the following constants for the prototypes of JavaScript built-in objects:
-
AggregateErrorPrototype
: the prototype of the JavaScript built-inAggregateError
objects, orundefined
if theAggregateError
class does not exist. -
ArrayBufferPrototype
: the prototype of the JavaScript built-inArrayBuffer
objects, orundefined
if theArrayBuffer
class does not exist. -
ArrayIteratorPrototype
: the prototype of the JavaScript built-in array iterator objects, orundefined
if the array iterator does not exist. -
BigInt64ArrayPrototype
: the prototype of the JavaScript built-inBigInt64Array
objects, orundefined
if theBigInt64Array
class does not exist. -
BigIntPrototype
: the prototype of the JavaScript built-inbigint
primitive, orundefined
if thebigint
primitive does not exist. -
BigUint64ArrayPrototype
: the prototype of the JavaScript built-inBigUint64Array
objects, orundefined
if theBigUint64Array
class does not exist. -
DataViewPrototype
: the prototype of the JavaScript built-inDataView
objects, orundefined
if theDataView
class does not exist. -
FinalizationRegistryPrototype
: the prototype of the JavaScript built-inFinalizationRegistry
objects, orundefined
if theFinalizationRegistry
class does not exist. -
Float32ArrayPrototype
: the prototype of the JavaScript built-inFloat32Array
objects, orundefined
if theFloat32Array
class does not exist. -
Float64ArrayPrototype
: the prototype of the JavaScript built-inFloat64Array
objects, orundefined
if theFloat64Array
class does not exist. -
Int16ArrayPrototype
: the prototype of the JavaScript built-inInt16Array
objects, orundefined
if theInt16Array
class does not exist. -
Int32ArrayPrototype
: the prototype of the JavaScript built-inInt32Array
objects, orundefined
if theInt32Array
class does not exist. -
Int8ArrayPrototype
: the prototype of the JavaScript built-inInt8Array
objects, orundefined
if theInt8Array
class does not exist. -
IntelSegmentIteratorPrototype
: the prototype of the JavaScript built-inIntl.SegmentIterator
objects, orundefined
if theIntl.SegmentIterator
class does not exist. -
InternalErrorPrototype
: the prototype of the JavaScript built-inInternalError
objects, orundefined
if theInternalError
class does not exist. -
IntlCollatorPrototype
: the prototype of the JavaScript built-inIntl.Collator
objects, orundefined
if theIntl.Collator
class does not exist. -
IntlDateTimeFormatPrototype
: the prototype of the JavaScript built-inIntl.DateTimeFormat
objects, orundefined
if theIntl.DateTimeFormat
class does not exist. -
IntlDisplayNamesPrototype
: the prototype of the JavaScript built-inIntl.DisplayNames
objects, orundefined
if theIntl.DisplayNames
class does not exist. -
IntlDurationFormatPrototype
: the prototype of the JavaScript built-inIntl.DurationFormat
objects, orundefined
if theIntl.DurationFormat
class does not exist. -
IntlListFormatPrototype
: the prototype of the JavaScript built-inIntl.ListFormat
objects, orundefined
if theIntl.ListFormat
class does not exist. -
IntlLocalePrototype
: the prototype of the JavaScript built-inIntl.Locale
objects, orundefined
if theIntl.Locale
class does not exist. -
IntlNumberFormatPrototype
: the prototype of the JavaScript built-inIntl.NumberFormat
objects, orundefined
if theIntl.NumberFormat
class does not exist. -
IntlPluralRulesPrototype
: the prototype of the JavaScript built-inIntl.PluralRules
objects, orundefined
if theIntl.PluralRules
class does not exist. -
IntlRelativeTimeFormatPrototype
: the prototype of the JavaScript built-inIntl.RelativeTimeFormat
objects, orundefined
if theIntl.RelativeTimeFormat
class does not exist. -
IntlSegmenterPrototype
: the prototype of the JavaScript built-inIntl.Segmenter
objects, orundefined
if theIntl.Segmenter
class does not exist. -
MapIteratorPrototype
: the prototype of the JavaScript built-inMap
iterator objects, orundefined
if theMap
iterator does not exist. -
MapPrototype
: the prototype of the JavaScript built-inMap
objects, orundefined
if theMap
class does not exist. -
PromisePrototype
: the prototype of the JavaScript built-inPromise
objects, orundefined
if thePromise
class does not exist. -
RegExpIteratorPrototype
: the prototype of the JavaScript built-inRegExp
iterator objects, orundefined
if theRegExp
iterator does not exist. -
RegExpPrototype
: the prototype of the JavaScript built-inRegExp
objects, orundefined
if theRegExp
class does not exist. -
SetIteratorPrototype
: the prototype of the JavaScript built-inSet
iterator objects, orundefined
if theSet
iterator does not exist. -
SetPrototype
: the prototype of the JavaScript built-inSet
objects, orundefined
if theSet
class does not exist. -
SharedArrayBufferPrototype
: the prototype of the JavaScript built-inSharedArrayBuffer
objects, orundefined
if theSharedArrayBuffer
class does not exist. -
StringIteratorPrototype
: the prototype of the JavaScript built-inString
iterator objects, orundefined
if theString
iterator does not exist. -
SymbolPrototype
: the prototype of the JavaScript built-inSymbol
primitives, orundefined
if theSymbol
primitive does not exist. -
Uint16ArrayPrototype
: the prototype of the JavaScript built-inUint16Array
objects, orundefined
if theUint16Array
class does not exist. -
Uint32ArrayPrototype
: the prototype of the JavaScript built-inUint32Array
objects, orundefined
if theUint32Array
class does not exist. -
Uint8ArrayPrototype
: the prototype of the JavaScript built-inUint8Array
objects, orundefined
if theUint8Array
class does not exist. -
Uint8ClampedArrayPrototype
: the prototype of the JavaScript built-inUint8ClampedArray
objects, orundefined
if theUint8ClampedArray
class does not exist. -
WeakMapPrototype
: the prototype of the JavaScript built-inWeakMap
objects, orundefined
if theWeakMap
class does not exist. -
WeakRefPrototype
: the prototype of the JavaScript built-inWeakRef
objects, orundefined
if theWeakRef
class does not exist. -
WeakSetPrototype
: the prototype of the JavaScript built-inWeakSet
objects, orundefined
if theWeakSet
class does not exist.
The following code shows how to use these constants:
import { WeakMapPrototype } from '@haixing_hu/typeinfo';
function foo(value) {
const proto = Object.getPrototypeOf(value);
if (proto === WeakMapPrototype) {
...
} else {
...
}
}
The library provides the following functions for type detection:
-
isArguments(value): boolean
: whether the specified value is the JavaScript built-inarguments
object. -
isBoolean(value): boolean
: whether the specified value is a JavaScript built-inboolean
primitive orBoolean
object. -
isBuffer(value): boolean
: whether the specified value is a JavaScript built-inArrayBuffer
orSharedArrayBuffer
object. -
isBuiltInClass(Class): boolean
: whether the specified class is a JavaScript built-in class. -
isCollection(value): boolean
: whether the specified value is a JavaScript built-in collection object, i.e., aMap
orSet
object. -
isError(value): boolean
: whether the specified value is an instance of the JavaScript built-inError
class, or an instance of a subclass of theError
class. -
isIntl(value): boolean
: whether the specified value is a JavaScript built-in object under theIntl
namespace. -
isIterator(value): boolean
: whether the specified value is an iterator object, i.e., an object with anext()
method. -
isNumeric(value): boolean
: whether the specified value is a JavaScript built-innumber
primitive, orbigint
primitive, orNumber
object. -
isString(value): boolean
: whether the specified value is a JavaScript built-instring
primitive, orString
object. -
isTypedArray(value): boolean
: whether the specified value is a JavaScript built-in typed array object. -
isWeak(value): boolean
: whether the specified value is a JavaScript built-in weak referenced object, i.e., aWeakMap
,WeakSet
, orWeakRef
object.
The following code shows how to use these functions:
import { isTypedArray } from '@haixing_hu/typeinfo';
function foo(value) {
if (isTypedArray(value)) {
...
} else {
...
}
}
One of the primary purposes of Proxy
objects in JavaScript is to allow developers
to customize the behavior of object operations, acting as a delegate for another
object (referred to as the target object). One of the key features of Proxy
is
its transparency—externally, unless the proxy object is intentionally designed
to reveal itself, it is challenging to distinguish a Proxy
object from the
target object it represents. This is largely because Proxy
can intercept and
redefine almost all fundamental operations of an object, including but not
limited to property access, assignment, and enumeration.
Therefore, when libraries like typeinfo attempt to retrieve type information
of an object, the inherent transparency of Proxy
means these libraries can
only process and "see" the final outcomes of operations, without direct means
to identify whether these operations were intercepted by a Proxy
. If a
Proxy
flawlessly mimics the behavior of its target object, there exists no
reliable method to determine from the operation outcomes whether an object is
a Proxy
. In essence, the design philosophy of Proxy
aims to make it nearly
invisible to external observation, making it impossible for even libraries
specialized in fetching type information to definitively ascertain if an object
is a Proxy
, unless the proxy object deliberately exposes its identity through
certain intercepting behaviors. This design significantly enhances the power
and flexibility of Proxy
, but it also means that directly detecting Proxy
objects through external observation presents a challenge.
If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request to the GitHub repository.
typeinfo is distributed under the Apache 2.0 license. See the LICENSE file for more details.