JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 20
  • Score
    100M100P100Q59957F
  • License MIT

Type detection beyond `typeof`

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.

    1. getType() now returns values in a case sensitive manner. Using lower case in v1 was meant to be more consistent with the behavior of typeof, but it made it impossible to distinguish between SomeObject and someobject.
    2. getType({ foo: 'bar' }) now returns PlainObject rather than object.
    3. getType(function* ()) now returns GeneratorFunction instead of generator.
    4. isGeneratorFunction() replaces isGenerator(); the latter has been kept as an alias for backwards compatibility, but is no longer part of this documentation.
    5. getType() now works around the issue with Function.name and returns the object type correctly. It will fall back to Object in 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 fixed Function.name issue [5]. As for getType() 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() and isWeakSet().

  • v2.2 introduces isCallable(). Not initiated classes are now reported as Class and no longer as Function. 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-type

Usage

What's the type comes with one generic detector, getType() and many is<Type>() functions:

// 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()); // true

Resources