1.2.10 • Published 3 years ago
what-is-that v1.2.10
what-is-that

Minimal, tiny (1.1 kb) object type identifier for both Node.js and browser written in TypeScript.
Feature
- Minimal: simply detect the type for any objects.
- Fast: it's basically an if statement.
- Tiny: only 1.1kb of gzipped.
compatible to ES3+
Install
npm install what-is-thatUsage
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)API
what(o: any): Type
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"
Detection
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 undefinedisNull(o: any): o is nullisNumber(o: any): o is numberisBoolean(o: any): o is booleanisString(o: any): o is stringisSymbol(o: any): o is SymbolisBigInt(o: any): o is BigIntisObject(o: any): o is Record<string, any>isObjectLike(o: any): o is Record<string | number | symbol, any>isFunction(o: any): o is FunctionisGeneratorFunction(o: any): o is GeneratorFunctionisGenerator(o: any): o is GeneratorisRegExp(o: any): o is RegExpisDate(o: any): o is DateisPromise(o: any): o is PromiseisError(o: any): o is ErrorisTextEncoder(o: any): o is TextEncoderisTextDecoder(o: any): o is TextDecoderisURL(o: any): o is URLisURLSearchParams(o: any): o is URLSearchParamsisArray(o: any): o is ArrayisSet(o: any): o is SetisMap(o: any): o is Map<any, any>isWeakSet(o: any): o is WeakSetisWeakMap(o: any): o is WeakMap<any, any>isDataView(o: any): o is DataViewisBuffer(o: any): o is BufferisArrayBuffer(o: any): o is ArrayBufferisSharedArrayBuffer(o: any): o is SharedArrayBufferisInt8Array(o: any): o is Int8ArrayisUint8Array(o: any): o is Uint8ArrayisUint8ClampedArray(o: any): o is Uint8ClampedArrayisInt16Array(o: any): o is Int16ArrayisUint16Array(o: any): o is Uint16ArrayisInt32Array(o: any): o is Int32ArrayisUint32Array(o: any): o is Uint32ArrayisFloat32Array(o: any): o is Float32ArrayisFloat64Array(o: any): o is Float64ArrayisBigInt64Array(o: any): o is BigInt64ArrayisBigUint64Array(o: any): o is BigUint64ArrayisBlob(o: any): o is BlobisFile(o: any): o is FileisUnknown(o: any): o is unknown
For TypeScript
There are some additional functions for generic object types which would be useful to ensure that the object is explicitly typed.
isObjectAs<T extends Record<string, any>>(o: any): o is TisObjectLikeAs<T extends Record<string | number | symbol, any>>(o:any): o is TisFunctionAs<T extends (...args: any[]) => any>(o: any): o is TisGeneratorAs(o: any): o is TisPromiseAs<T extends Promise>(o: any): o is TisArrayAs<T extends Array>(o: any): o is TisSetAs<T extends Set>(o: any): o is TisMapAs<T extends Map<any, any>>(o: any): o is TisWeakSetAs<T extends WeakSet>(o: any): o is TisWeakMapAs<T extends WeakMap<any, any>>(o: any): o is TisUnknownAs(o: any): o is T