CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-is-type-of

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

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

standard-objects.mddocs/

Standard Object Type Checking

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

Capabilities

Array Type Checking

Returns true if value is an array (doesn't check array items).

/**
 * Returns true if val is array, it won't check items of array
 * @param val - Value to check
 * @returns Type guard indicating if value is array
 */
function isArray<T = any>(val?: unknown): val is Array<T>;

Usage Example:

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

isArray([1, 2, 3]);     // => true
isArray("hello");       // => false
isArray({length: 3});   // => false

// With type parameter
function processNumbers(value: unknown) {
  if (isArray<number>(value)) {
    // value is now typed as number[]
    value.forEach(n => console.log(n * 2));
  }
}

Function Type Checking

Returns true if value is a function.

/**
 * Returns true if val is function
 * @param val - Value to check
 * @returns Type guard indicating if value is function
 */
function isFunction<T extends Function>(val?: unknown): val is T;

Usage Example:

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

isFunction(() => {});           // => true
isFunction(function() {});      // => true
isFunction(Math.max);           // => true
isFunction("function");         // => false

Generator Function Type Checking

Returns true if value is a generator function.

/**
 * Returns true if val is generator function
 * @param val - Value to check
 * @returns Type guard indicating if value is generator function
 */
function isGeneratorFunction(val?: unknown): val is GeneratorFunction;

Usage Example:

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

function* generator() { yield 1; }

isGeneratorFunction(generator);     // => true
isGeneratorFunction(() => {});      // => false

Async Function Type Checking

Returns true if value is an async function.

/**
 * Returns true if val is async function
 * @param val - Value to check
 * @returns Type guard indicating if value is async function
 */
function isAsyncFunction(val?: unknown): val is Function;

Usage Example:

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

async function asyncFn() { return 42; }

isAsyncFunction(asyncFn);       // => true
isAsyncFunction(() => {});      // => false

Async Generator Function Type Checking

Returns true if value is an async generator function.

/**
 * Returns true if val is async generator function
 * @param val - Value to check
 * @returns Type guard indicating if value is async generator function
 */
function isAsyncGeneratorFunction(val?: unknown): val is AsyncGeneratorFunction;

Usage Example:

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

async function* asyncGenerator() { yield 1; }

isAsyncGeneratorFunction(asyncGenerator);  // => true
isAsyncGeneratorFunction(() => {});        // => false

Object Type Checking

Returns true if value is an object (and not null).

/**
 * Returns true if val is object
 * @param val - Value to check
 * @returns Type guard indicating if value is object
 */
function isObject(val?: unknown): val is object;

Usage Example:

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

isObject({});           // => true
isObject([]);           // => true
isObject(null);         // => false
isObject("string");     // => false

Class Type Checking

Returns true if value is a class constructor.

/**
 * Returns true if val is class
 * Note: "class" is supported in ECMAScript 6, and if the code is using some compiler or transpiler, the checking might fail
 * @param val - Value to check
 * @returns Type guard indicating if value is class
 */
function isClass<T extends Class>(val?: unknown): val is T;

Usage Example:

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

class MyClass {}

isClass(MyClass);       // => true
isClass(() => {});      // => false

Regular Expression Type Checking

Returns true if value is a regular expression.

/**
 * Returns true if val is regular expression
 * @param val - Value to check
 * @returns Type guard indicating if value is RegExp
 */
function isRegExp(val?: unknown): val is RegExp;

Usage Example:

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

isRegExp(/pattern/);        // => true
isRegExp(new RegExp("pattern"));  // => true
isRegExp("pattern");        // => false

Date Type Checking

Returns true if value is a Date instance.

/**
 * Returns true if val is instance of Date
 * @param val - Value to check
 * @returns Type guard indicating if value is Date
 */
function isDate(val?: unknown): val is Date;

Usage Example:

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

isDate(new Date());         // => true
isDate("2023-01-01");       // => false
isDate(Date.now());         // => false

Error Type Checking

Returns true if value is an Error instance.

/**
 * Returns true if val is instance of Error
 * @param val - Value to check
 * @returns Type guard indicating if value is Error
 */
function isError(val?: unknown): val is Error;

Usage Example:

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

isError(new Error("message"));      // => true
isError(new TypeError("message"));  // => true
isError("error message");           // => false

Generator Type Checking

Returns true if value is a generator object.

/**
 * Returns true if val is generator
 * @param val - Value to check
 * @returns Type guard indicating if value is Generator
 */
function isGenerator(val?: unknown): val is Generator;

Usage Example:

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

function* generatorFn() { yield 1; }
const gen = generatorFn();

isGenerator(gen);           // => true
isGenerator(generatorFn);   // => false

Promise Type Checking

Returns true if value is a Promise.

/**
 * Returns true if val is promise
 * @param val - Value to check
 * @returns Type guard indicating if value is Promise
 */
function isPromise<T = any>(val?: unknown): val is Promise<T>;

Usage Example:

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

isPromise(Promise.resolve(42));     // => true
isPromise(new Promise(() => {}));   // => true
isPromise({then: () => {}});        // => false (use isPromiseLike for this)

Promise-like Type Checking

Returns true if value is promise-like (has a 'then' method).

/**
 * Returns true if val is like promise, if the object has then property, the checking will pass
 * @param val - Value to check
 * @returns Type guard indicating if value is PromiseLike
 */
function isPromiseLike<T = any>(val?: unknown): val is PromiseLike<T>;

Usage Example:

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

const thenable = {
  then: (onResolve: Function) => onResolve(42)
};

isPromiseLike(Promise.resolve(42));   // => true
isPromiseLike(thenable);              // => true
isPromiseLike({});                    // => false

Type Definitions

type Class = new (...args: any[]) => any;

docs

external-objects.md

index.md

node-objects.md

primitive-types.md

standard-objects.md

tile.json