Registry / testing / is-type-of

is-type-of

JSON →
library2.2.0jsnpmunverified

is-type-of is a comprehensive utility library for performing various type checks on JavaScript values, primarily targeting Node.js environments. It offers a wide array of functions to identify primitive types (like string, number, boolean, bigint, symbol, null, undefined), standard JavaScript objects (array, function, object), and Node.js-specific constructs such as generator functions and async functions. The package ships with full TypeScript support, including type guards, enhancing development with strong typing. The current stable version is 2.2.0, released in December 2024, which includes Node.js 14 support for `hasOwn`. The library maintains a regular release cadence, with version 2.1.0 (November 2023) introducing dual CommonJS and ES Module support using `tshy`, ensuring broad compatibility. Its key differentiators include its extensive and specific type checking methods and seamless TypeScript integration, making it a reliable tool for robust type validation.

npm install is-type-of
INSTALL
IMPORT
SIG · IS-TYPE-OF
I
is-type-of
testingjavascriptv2.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

is
import is from 'is-type-of';
import { is } from 'is-type-of';
The primary `is` object is exported as a default. Use default import for the main utility object.
isArray
import { isArray } from 'is-type-of';
import isArray from 'is-type-of';
Individual type checking functions (e.g., `isArray`, `isString`) are named exports. Do not use default import for these.
is
const is = require('is-type-of');
const { isArray } = require('is-type-of');
Since v2.1.0, the package supports both ESM and CommonJS. For CommonJS, `require('is-type-of')` returns the main `is` object, which contains all type-checking methods as properties (e.g., `is.isArray`). Direct destructuring of named exports via `require` is not the intended pattern; use `is.isArray` instead.
BigInt
import type { BigInt } from 'is-type-of';
For TypeScript users, type definitions are included. Specific types for type guards are inferred or can be imported if needed for advanced scenarios, though direct type imports like `BigInt` are less common for a utility focused on runtime checks.

Demonstrates both default and named imports for various type checks, including basic primitives, objects, generator functions, and BigInt. It also illustrates the usage of TypeScript type guards provided by the library.

import is, { isArray, isString } from 'is-type-of'; console.log('--- Using default import (is.method) ---'); console.log('Is [] an array?', is.array([])); console.log('Is 123 a number?', is.number(123)); console.log('Is "hello" a string?', is.string('hello')); console.log('Is {} an object?', is.object({})); console.log('Is function*() {} a generator function?', is.generatorFunction(function* () {})); console.log('\n--- Using named imports (isMethod) ---'); console.log('Is [] an array (named)?', isArray([])); console.log('Is "world" a string (named)?', isString('world')); function processData(data: string[] | string) { // TypeScript type guard example if (isArray(data)) { console.log(`\nProcessing array of length: ${data.length}`); data.forEach(item => console.log(` - Array item: ${item}`)); } else { console.log(`\nProcessing string: ${data}`); } } processData(['apple', 'banana']); processData('orange'); // Example with a less common type check const bigIntValue = BigInt(9007199254740991); console.log(`\nIs ${bigIntValue} a BigInt?`, is.bigInt(bigIntValue));
Debug
Known issues
gotchaThe `is.array()` and `isArray()` functions only check if the value itself is an array, not the types of its elements. Developers expecting deep array type validation will need additional logic.
fix
Manually iterate through array elements and apply `is.typeOf()` checks or use a third-party deep validation library if element-level validation is required.
affects: >=1.0.0
gotchaThe library explicitly differentiates between primitive types (e.g., `string`, `number`, `boolean`) and their corresponding object wrappers (`String`, `Number`, `Boolean`). Functions like `is.string()` will return `false` for `new String('value')`.
fix
Always use primitive literals for values expected to be primitive types. If object wrappers might be encountered, convert them to primitives (`String(value)`) or check for both (`is.string(val) || is.object(val) && val instanceof String`).
affects: >=1.0.0
gotchaWhile `is-type-of` supports both ES Modules (ESM) and CommonJS (CJS) since v2.1.0, incorrect import syntax can still lead to runtime errors, particularly when mixing module styles or when TypeScript's `esModuleInterop` is not configured correctly. The package's CommonJS export structure is primarily a default export containing all methods.
fix
For ESM, use `import is from 'is-type-of'` for the main object and `import { isArray } from 'is-type-of'` for individual helpers. For CJS, use `const is = require('is-type-of')` and access methods via `is.isArray`. Ensure `"esModuleInterop": true` in `tsconfig.json` for TypeScript projects consuming CJS packages that export a default.
affects: >=2.1.0
Errors
Common errors & fixes
TypeError: is_type_of_1.isArray is not a function
Attempting to access a named export like `isArray` on the default imported `is` object, often due to incorrect import syntax when using ESM or TypeScript's default transpilation.
fix
If importing the main `is` object, access the method as `is.isArray(value)`. If you want `isArray` as a direct function, use named import: `import { isArray } from 'is-type-of';`.
TypeError: Cannot read properties of undefined (reading 'array') (or similar for other methods)
The default `is` object was not correctly imported, resulting in `is` being `undefined` or not having the expected properties.
fix
Ensure `import is from 'is-type-of';` (for ESM/TypeScript) or `const is = require('is-type-of');` (for CommonJS) is used to correctly import the main utility object.
ERR_REQUIRE_ESM
Attempting to `require()` an ES Module in a CommonJS context, or subtle module resolution issues when a project mixes ESM and CJS.
fix
Since v2.1.0, `is-type-of` is built with `tshy` for dual ESM/CJS support. Ensure your Node.js version is recent enough to handle dual packages correctly (Node.js 12+ usually suffices, but Node.js 14+ is recommended for full feature set). If possible, refactor your consuming code to use `import` syntax if your project is ESM-first, or ensure your bundler/loader is correctly configured for dual-package consumption.
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
is-type-of — npm install is-type-of · libregistry