is-type-of: Complete Type Checking for Node.js

2.2.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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));

view raw JSON →