Robust JavaScript Type Detection
type-of-is provides a reliable mechanism for JavaScript type detection and comparison, addressing the well-known quirks of the native `typeof` operator (e.g., `typeof null` being 'object', or arrays being 'object'). It uses a combination of `Object.prototype.toString` and constructor checks to determine the true type of a value, including primitives, built-in objects, and custom constructors. The package is currently at version 3.5.1, though the latest publicly listed version on npm (3.4.0) was published in February 2017, suggesting a very slow or effectively halted release cadence. Its key differentiator is its commitment to 'sensible / unsurprising' type results, making it suitable for validation and conditional logic where native `typeof` falls short. It offers functions like `Type.of` to get the constructor, `Type.string` for a string representation, and `Type.is` for comparison against a type or string.
Common errors
-
ReferenceError: require is not defined (in ESM context)
cause Attempting to use `const Type = require('type-of-is');` in an ES Module (`.mjs` file or `type: 'module'` project) context.fixChange the import statement to `import Type from 'type-of-is';`. -
TypeError: Type.of is not a function (when using named import)
cause Incorrectly importing `Type` as a named export (e.g., `import { Type } from 'type-of-is';`) or a wildcard import (e.g., `import * as Type from 'type-of-is';`) instead of a default import, in a way that doesn't correctly resolve the default export.fixEnsure `Type` is imported as a default export: `import Type from 'type-of-is';`. -
console.log(Type.is([], Object)); // false (unexpected for some users)
cause Misunderstanding `Type.is` behavior: it primarily checks for a direct constructor match rather than `instanceof` behavior (i.e., checking against the prototype chain). An array's direct constructor is `Array`, not `Object`.fixIf you intend to check inheritance (e.g., if an array is ultimately an `Object`), use `Type.instance([], Object)` which will return `true` as it leverages JavaScript's `instanceof` operator. `Type.is` is for more specific, direct type comparisons.
Warnings
- gotcha The `Type.of()` method returns `null` for `null` and `undefined` for `undefined`. This differs from `Type.string()` which returns the string literals "null" and "undefined" respectively. Be mindful of which method you use based on whether you need the actual primitive value or its string representation.
- deprecated The npm registry lists version 3.4.0 as the latest public release, last published on February 7, 2017. While the package.json indicates version 3.5.1, the lack of public updates in over 7 years suggests the package might be unmaintained or have a very inactive development cycle. Use with caution for new projects requiring active support or frequent updates.
- gotcha By default, `Type.is(obj, type)` checks for direct constructor match, not inheritance. For example, `Type.is(new CustomObject(), Object)` will return `false`. If you need to check if an object is an instance of a class anywhere in its prototype chain, use `Type.instance(obj, type)` (which is a wrapper around `instanceof`).
Install
-
npm install type-of-is -
yarn add type-of-is -
pnpm add type-of-is
Imports
- Type
import { Type } from 'type-of-is'; // or import * as Type from 'type-of-is';import Type from 'type-of-is';
- Type.of
import { of } from 'type-of-is';import Type from 'type-of-is'; const typeName = Type.of({}); - Type
const Type = require('type-of-is');
Quickstart
import Type from 'type-of-is';
// Determine type using Type.of() or Type(single_argument)
console.log('Type.of("hello"):', Type.of('hello')); // [Function: String]
console.log('Type(123):', Type(123)); // [Function: Number]
console.log('Type.of([]):', Type.of([])); // [Function: Array]
console.log('Type.of(null):', Type.of(null)); // null
console.log('Type.of(undefined):', Type.of(undefined)); // undefined
// Get type as a string using Type.string()
console.log('Type.string("world"):', Type.string('world')); // "String"
console.log('Type.string({}):', Type.string({})); // "Object"
console.log('Type.string(null):', Type.string(null)); // "null"
// Test if an object is of a specific type using Type.is() or Type(obj, type)
function CustomObject() {}
const myObj = new CustomObject();
console.log('Type.is(true, Boolean):', Type.is(true, Boolean)); // true
console.log('Type.is("test", "String"):', Type.is('test', 'String')); // true
console.log('Type.is(myObj, CustomObject):', Type.is(myObj, CustomObject)); // true
console.log('Type.is(myObj, Object):', Type.is(myObj, Object)); // false (Type.is checks direct constructor, not prototype chain by default)
// Type.instance() wraps instanceof
console.log('Type.instance(myObj, CustomObject):', Type.instance(myObj, CustomObject)); // true
console.log('Type.instance(myObj, Object):', Type.instance(myObj, Object)); // true