Minimal, tiny (1.1 kb) object type identifier for both Node.js and browser written in TypeScript.
npm install what-is-thatMinimal, tiny (1.1 kb) object type identifier for both Node.js and browser written in TypeScript.
- Minimal: simply detect the type for any objects.
- Fast: it's basically an if statement.
- Tiny: only 1.1kb of gzipped.
compatible to ES3+
``sh`
npm install what-is-that
`ts
import {what, isObject, isObjectLike} from 'what-is-that'
// 'Boolean'
what(true)
// 'String'
what('that')
/* Detect whether it's a Plain object or any other (ex. custom class instance) /
// 'Object'
what({is: 'that'})
class MyObj {}
const obj = new MyObj()
// 'ObjectLike'
what(obj)
// false
isObject(obj)
// true
isObjectLike(obj)
`
Returns a string representation of the type of argument or "Unknown" if it can't be identified.
The return value will be one of the list below.
- "Undefined""Null"
- "Number"
- "Boolean"
- "String"
- "Symbol"
- "BigInt"
- "Object"
- "ObjectLike"
- "Function"
- "GeneratorFunction"
- "Generator"
- "RegExp"
- "Date"
- "Promise"
- "Error"
- "TextEncoder"
- "TextDecoder"
- "URL"
- "URLSearchParams"
- "Array"
- "Set"
- "Map"
- "WeakSet"
- "WeakMap"
- "DataView"
- "Buffer"
- "ArrayBuffer"
- "SharedArrayBuffer"
- "Int8Array"
- "Uint8Array"
- "Uint8ClampedArray"
- "Int16Array"
- "Uint16Array"
- "Int32Array"
- "Uint32Array"
- "Float32Array"
- "Float64Array"
- "BigInt64Array"
- "BigUint64Array"
- "Blob"
- "File"
- "Unknown"
-
Functions which return true if the argument is a value or instance of the corresponding object type, false if not.
- isUndefined (o: any): o is undefined
- isNull (o: any): o is null
- isNumber (o: any): o is number
- isBoolean (o: any): o is boolean
- isString (o: any): o is string
- isSymbol (o: any): o is Symbol
- isBigInt (o: any): o is BigInt
- isObject (o: any): o is Record
- isObjectLike (o: any): o is Record
- isFunction (o: any): o is Function
- isGeneratorFunction (o: any): o is GeneratorFunction
- isGenerator (o: any): o is Generator
- isRegExp (o: any): o is RegExp
- isDate (o: any): o is Date
- isPromise (o: any): o is Promise
- isError (o: any): o is Error
- isTextEncoder (o: any): o is TextEncoder
- isTextDecoder (o: any): o is TextDecoder
- isURL (o: any): o is URL
- isURLSearchParams (o: any): o is URLSearchParams
- isArray (o: any): o is Array
- isSet (o: any): o is Set
- isMap (o: any): o is Map
- isWeakSet (o: any): o is WeakSet
- isWeakMap (o: any): o is WeakMap
- isDataView (o: any): o is DataView
- isBuffer (o: any): o is Buffer
- isArrayBuffer (o: any): o is ArrayBuffer
- isSharedArrayBuffer (o: any): o is SharedArrayBuffer
- isInt8Array (o: any): o is Int8Array
- isUint8Array (o: any): o is Uint8Array
- isUint8ClampedArray (o: any): o is Uint8ClampedArray
- isInt16Array (o: any): o is Int16Array
- isUint16Array (o: any): o is Uint16Array
- isInt32Array (o: any): o is Int32Array
- isUint32Array (o: any): o is Uint32Array
- isFloat32Array (o: any): o is Float32Array
- isFloat64Array (o: any): o is Float64Array
- isBigInt64Array (o: any): o is BigInt64Array
- isBigUint64Array (o: any): o is BigUint64Array
- isBlob (o: any): o is Blob
- isFile (o: any): o is File
- isUnknown (o: any): o is unknown
There are some additional functions for generic object types which would be useful to ensure that the object is explicitly typed.
- isObjectAs
- isObjectLikeAs
- isFunctionAs
- isGeneratorAs
- isPromiseAs
- isArrayAs
- isSetAs
- isMapAs
- isWeakSetAs
- isWeakMapAs
- isUnknownAs`