or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

external-objects.mdindex.mdnode-objects.mdprimitive-types.mdstandard-objects.md
tile.json

tessl/npm-is-type-of

Complete type checking utility library for Node.js with TypeScript support and type guards

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-type-of@2.2.x

To install, run

npx @tessl/cli install tessl/npm-is-type-of@2.2.0

index.mddocs/

is-type-of

is-type-of is a comprehensive type checking utility library for Node.js that provides complete type validation including primitive types, standard objects, and Node.js specific objects. It offers both ES Module and CommonJS compatibility with full TypeScript support including type guards for enhanced type safety in TypeScript applications.

Package Information

  • Package Name: is-type-of
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install is-type-of

Core Imports

import is from "is-type-of";
import { isArray, isString, isNumber } from "is-type-of";

For CommonJS:

const is = require("is-type-of");
const { isArray, isString } = require("is-type-of");

Basic Usage

import is from "is-type-of";
import { isArray, isString } from "is-type-of";

// Using the main is object
is.array([1, 2, 3]); // => true
is.string("hello"); // => true
is.primitive(42); // => true
is.primitive({}); // => false

// Using named imports
isArray([1, 2, 3]); // => true
isString("hello"); // => true

// Type guard usage in TypeScript
function processValue(value: string | number[]) {
  if (isArray(value)) {
    // value is now typed as number[]
    value.forEach(console.log);
  } else {
    // value is now typed as string
    console.log(value.toUpperCase());
  }
}

Architecture

is-type-of is organized around several key concepts:

  • Central is Object: Main namespace containing all type checking methods organized by category
  • Named Exports: Individual functions for direct imports and tree-shaking
  • Type Guards: All functions act as TypeScript type guards for compile-time type safety
  • Legacy Compatibility: Deprecated exports maintain backward compatibility
  • Comprehensive Coverage: Support for primitives, standard objects, Node.js objects, and external libraries

Capabilities

Primitive Type Checking

Complete type checking for JavaScript primitive types including strings, numbers, booleans, symbols, null, undefined, and bigint. Includes specialized number checkers for integers, safe integers, and doubles.

// Core primitive checkers
function isString(val?: unknown): val is string;
function isNumber(val?: unknown): val is number;
function isBoolean(val?: unknown): val is boolean;
function isSymbol(val?: unknown): val is symbol;
function isUndefined(val?: unknown): val is undefined;
function isNull(val?: unknown): val is null;
function isBigInt(val?: unknown): val is bigint;

// Composite primitive checkers
function isNullable(val?: unknown): val is null | undefined;
function isPrimitive(val?: unknown): val is Primitive;

// Number specializations
function isInteger(val?: unknown): val is number;
function isInteger32(val?: unknown): val is number;
function isLong(val?: unknown): val is number;
function isSafeInteger(val?: unknown): val is number;
function isDouble(val?: unknown): val is number;
function isNaN(val?: unknown): boolean;
function isFinite(val?: unknown): val is number;

Primitive Types

Standard Object Type Checking

Type checking for standard JavaScript objects including arrays, functions, classes, dates, errors, regular expressions, generators, and promises.

// Core object checkers
function isArray<T = any>(val?: unknown): val is Array<T>;
function isFunction<T extends Function>(val?: unknown): val is T;
function isObject(val?: unknown): val is object;
function isDate(val?: unknown): val is Date;
function isError(val?: unknown): val is Error;
function isRegExp(val?: unknown): val is RegExp;

// Function specializations
function isGeneratorFunction(val?: unknown): val is GeneratorFunction;
function isAsyncFunction(val?: unknown): val is Function;
function isAsyncGeneratorFunction(val?: unknown): val is AsyncGeneratorFunction;
function isClass<T extends Class>(val?: unknown): val is T;

// Advanced objects
function isGenerator(val?: unknown): val is Generator;
function isPromise<T = any>(val?: unknown): val is Promise<T>;
function isPromiseLike<T = any>(val?: unknown): val is PromiseLike<T>;

Standard Objects

Node.js Object Type Checking

Specialized type checking for Node.js specific objects including buffers and streams with support for readable, writable, and duplex streams.

function isBuffer(val: unknown): val is Buffer;
function isStream(val?: unknown): val is Stream;
function isReadable(val?: unknown): val is Readable;
function isWritable(val?: unknown): val is Writable;
function isDuplex(val?: unknown): val is Duplex;

Node.js Objects

External Object Type Checking

Type checking for objects from external libraries, currently supporting Long objects from the 'long' npm package.

function isLongObject(obj?: unknown): obj is LongObject;

interface LongObject {
  high: number;
  low: number;
}

External Objects

Utility Functions

Type-safe utility functions for advanced type checking scenarios.

/**
 * Returns true if val is an instance of the given class constructor
 * @param val - Value to check
 * @param Clazz - Constructor function to check against
 * @returns Type guard indicating if value is instance of Clazz
 */
function isInstanceOf<T extends Class>(val: unknown, Clazz: T): val is InstanceType<T>;

Usage Example:

import { isInstanceOf } from "is-type-of";

class MyClass {
  value: number;
  constructor(value: number) {
    this.value = value;
  }
}

const instance = new MyClass(42);
const obj = { value: 42 };

isInstanceOf(instance, MyClass); // => true
isInstanceOf(obj, MyClass);      // => false

// Type guard usage
function processValue(val: unknown) {
  if (isInstanceOf(val, MyClass)) {
    // val is now typed as MyClass
    console.log(val.value);
  }
}

Central is Object

All type checking functions are available through the central is object:

const is: {
  // Primitive types
  boolean: typeof isBoolean;
  number: typeof isNumber;
  string: typeof isString;
  symbol: typeof isSymbol;
  undefined: typeof isUndefined;
  null: typeof isNull;
  nullable: typeof isNullable;
  bigInt: typeof isBigInt;
  primitive: typeof isPrimitive;
  integer: typeof isInteger;
  integer32: typeof isInteger32;
  long: typeof isLong;
  double: typeof isDouble;
  finite: typeof isFinite;
  NaN: typeof isNaN;
  safeInteger: typeof isSafeInteger;

  // Standard objects
  function: typeof isFunction;
  generatorFunction: typeof isGeneratorFunction;
  asyncFunction: typeof isAsyncFunction;
  asyncGeneratorFunction: typeof isAsyncGeneratorFunction;
  class: typeof isClass;
  array: typeof isArray;
  object: typeof isObject;
  date: typeof isDate;
  error: typeof isError;
  regExp: typeof isRegExp;
  generator: typeof isGenerator;
  promise: typeof isPromise;
  promiseLike: typeof isPromiseLike;

  // Node.js objects
  buffer: typeof isBuffer;
  stream: typeof isStream;
  readable: typeof isReadable;
  writable: typeof isWritable;
  duplex: typeof isDuplex;

  // External objects
  longObject: typeof isLongObject;
};

Type Definitions

type Primitive = string | number | bigint | boolean | symbol | null | undefined;
type Nullable = null | undefined;
type Class = new (...args: any[]) => any;

interface LongObject {
  high: number;
  low: number;
}