is-type-of: Complete Type Checking for Node.js
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.
Common errors
-
TypeError: is_type_of_1.isArray is not a function
cause 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.fixIf 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)
cause The default `is` object was not correctly imported, resulting in `is` being `undefined` or not having the expected properties.fixEnsure `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
cause Attempting to `require()` an ES Module in a CommonJS context, or subtle module resolution issues when a project mixes ESM and CJS.fixSince 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.
Warnings
- gotcha The `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.
- gotcha The 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')`.
- gotcha While `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.
Install
-
npm install is-type-of -
yarn add is-type-of -
pnpm add is-type-of
Imports
- is
import { is } from 'is-type-of';import is from 'is-type-of';
- isArray
import isArray from 'is-type-of';
import { isArray } from 'is-type-of'; - is
const { isArray } = require('is-type-of');const is = require('is-type-of'); - BigInt
import type { BigInt } from 'is-type-of';
Quickstart
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));