Package Exports
- whats-the-type
- whats-the-type/isArray.js
- whats-the-type/isNull.js
- whats-the-type/isPlainObject.js
- whats-the-type/isUndefined.js
- whats-the-type/whats-the-type.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (whats-the-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
What's the type?
typeof returns [object Object] for almost everything beyond primitives. This module works around this drawback and returns a more accurate type.
What's new in v2?
v2 brings some improvements but also introduces a breaking change by going case sensitive.
getType()now returns values in a case sensitive manner. Using lower case in v1 was meant to be more consistent with the behavior oftypeof, but it made it impossible to distinguish betweenSomeObjectandsomeobject.getType({ foo: 'bar' })now returnsPlainObjectrather thanobject.getType(function* ())now returnsGeneratorFunctioninstead ofgenerator.isGeneratorFunction()replacesisGenerator(); the latter has been kept as an alias for backwards compatibility, but is no longer part of this documentation.getType()now works around the issue withFunction.nameand returns the object type correctly. It will fall back toObjectin the case of failure.
If you're using the
is<Type>()functions only, your code will keep on working without any changes, except for the now fixedFunction.nameissue [5]. As forgetType()you'll need to update your code with regards to the case sensitivity [1].v2.1 adds a number of
is<Type>()functions:isAsyncFunction(),isBlob(),isIterable(),isPromise(),isWeakMap()andisWeakSet().v2.2 introduces
isCallable(). Not initiated classes are now reported asClassand no longer asFunction. It also fixes some minor bugs from earlier versions, mainly in the documentation.v2.3 removes
isFile()again, because File objects and physical files aren't the same thing. This could have lead to wrong expectations and misunderstandings. v2.3 also adds more code examples.
Installation
npm install whats-the-typeUsage
What's the type comes with one generic detector, getType() and many is<Type>() functions:
- isArray()
- isAsyncFunction()
- isBigInt()
- isBlob()
- isBoolean()
- isCallable()
- isDate()
- isError()
- isFunction()
- isGeneratorFunction()
- isMap()
- isNull()
- isNumber()
- isPlainObject()
- isPromise()
- isRegExp()
- isSet()
- isString()
- isSymbol()
- isUndefined()
- isWeakMap()
- isWeakSet()
// Import 'whats-the-type' if you have a bunch of different types to check. This imports all functions at once.
import detector from 'whats-the-type'; // note that whats-the-type is implemented as ESM and not in CJS
// use `getType` if you don't know what type to expect
import getType from 'whats-the-type/getType.js';
// use the `is<Type>` functions if you want to confirm if a value is of a specific type
import isString from 'whats-the-type/isString.js';getType()
getType() returns a string such as null and undefined, Function, String, etc. These values are case sensitive and mostly correspond to the constructor names. In other words, the function doesn't just return object for most types but is as precise as possible.
// the detector always returns a string
getType(['a', 'b', 'c']); // 'Array'
// use the import name (e.g. 'detector') if you imported everything
detector.getType(123); // 'Number'
// plain objects are reported as 'PlainObject' rather than the more ambiguous 'Object'
getType({}); // 'PlainObject'
// custom classes are reported by their name
class MyClass extends Object {};
getType(new MyClass()); // 'MyClass'is<Type>()
is<Type>() functions return true if the value is of the specified type.
isString('a'); // true
isString(123); // false
isPlainObject({ a: 1 }); // true
isMap(new Map()); // true
isCallable(() => {}); // true
isAsyncFunction(async () => {}); // true
isGeneratorFunction(function* () {}); // true
isWeakMap(new WeakMap()); // true
isWeakSet(new WeakSet()); // trueResources
- Repository: github.com/draber/whats-the-type
- Package: npmjs.com/package/whats-the-type
- Docs: whats-the-type.netlify.app