{"id":14992,"library":"type-of-is","title":"Robust JavaScript Type Detection","description":"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.","status":"maintenance","version":"3.5.1","language":"javascript","source_language":"en","source_url":"https://github.com/stephenhandley/type-of-is","tags":["javascript","type","types","typeof","toString","type.of","type.is"],"install":[{"cmd":"npm install type-of-is","lang":"bash","label":"npm"},{"cmd":"yarn add type-of-is","lang":"bash","label":"yarn"},{"cmd":"pnpm add type-of-is","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses a default export pattern where the main 'Type' object/function is exported. Attempting named imports or wildcard imports might lead to unexpected behavior if not configured for CommonJS interop. Since v3.x, it should support both CJS and ESM.","wrong":"import { Type } from 'type-of-is';\n// or\nimport * as Type from 'type-of-is';","symbol":"Type","correct":"import Type from 'type-of-is';"},{"note":"Methods like `of`, `is`, and `string` are properties of the main `Type` export, not individual named exports. They must be accessed via the imported `Type` object.","wrong":"import { of } from 'type-of-is';","symbol":"Type.of","correct":"import Type from 'type-of-is';\nconst typeName = Type.of({});"},{"note":"This is the original and most documented import style for CommonJS environments.","symbol":"Type","correct":"const Type = require('type-of-is');"}],"quickstart":{"code":"import Type from 'type-of-is';\n\n// Determine type using Type.of() or Type(single_argument)\nconsole.log('Type.of(\"hello\"):', Type.of('hello')); // [Function: String]\nconsole.log('Type(123):', Type(123));             // [Function: Number]\nconsole.log('Type.of([]):', Type.of([]));           // [Function: Array]\nconsole.log('Type.of(null):', Type.of(null));       // null\nconsole.log('Type.of(undefined):', Type.of(undefined)); // undefined\n\n// Get type as a string using Type.string()\nconsole.log('Type.string(\"world\"):', Type.string('world')); // \"String\"\nconsole.log('Type.string({}):', Type.string({}));         // \"Object\"\nconsole.log('Type.string(null):', Type.string(null));     // \"null\"\n\n// Test if an object is of a specific type using Type.is() or Type(obj, type)\nfunction CustomObject() {}\nconst myObj = new CustomObject();\n\nconsole.log('Type.is(true, Boolean):', Type.is(true, Boolean)); // true\nconsole.log('Type.is(\"test\", \"String\"):', Type.is('test', 'String')); // true\nconsole.log('Type.is(myObj, CustomObject):', Type.is(myObj, CustomObject)); // true\nconsole.log('Type.is(myObj, Object):', Type.is(myObj, Object)); // false (Type.is checks direct constructor, not prototype chain by default)\n\n// Type.instance() wraps instanceof\nconsole.log('Type.instance(myObj, CustomObject):', Type.instance(myObj, CustomObject)); // true\nconsole.log('Type.instance(myObj, Object):', Type.instance(myObj, Object));   // true","lang":"javascript","description":"Demonstrates core type detection, string representation, and type comparison functionalities using both direct methods and the shorthand `Type()` function."},"warnings":[{"fix":"Use `Type.of(value)` when you need the constructor function (or primitive value for null/undefined). Use `Type.string(value)` when you need a consistent string name for the type.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate the stability requirements for your project. Consider alternative, actively maintained type-checking libraries if continuous updates and community support are critical. If using, thoroughly test against your specific use cases.","message":"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.","severity":"deprecated","affected_versions":">=3.4.0"},{"fix":"For direct constructor matching, use `Type.is(obj, Constructor)`. For checking against the prototype chain (like `instanceof`), use `Type.instance(obj, Constructor)`.","message":"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`).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change the import statement to `import Type from 'type-of-is';`.","cause":"Attempting to use `const Type = require('type-of-is');` in an ES Module (`.mjs` file or `type: 'module'` project) context.","error":"ReferenceError: require is not defined (in ESM context)"},{"fix":"Ensure `Type` is imported as a default export: `import Type from 'type-of-is';`.","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.","error":"TypeError: Type.of is not a function (when using named import)"},{"fix":"If 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.","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`.","error":"console.log(Type.is([], Object)); // false (unexpected for some users)"}],"ecosystem":"npm"}