is-lite: Lightweight Type Checking

2.0.0 · active · verified Tue Apr 21

is-lite is a concise and efficient JavaScript utility library for performing type checks and validations, currently at version 2.0.0. It offers a comprehensive suite of functions for identifying various data types, ranging from primitives like `string` and `number` to complex structures such as `Array`, `Map`, `class` constructors, and asynchronous functions. A core strength of is-lite is its robust TypeScript support, which includes type guards that facilitate accurate type inference within conditional statements, thereby improving code safety and developer experience. The library is designed to be lightweight, providing a minimal bundle size, and supports tree-shakeable, individual imports for each type checker from the `is-lite/standalone` path, optimizing performance in bundle-conscious environments. It maintains a consistent release cadence, with frequent updates incorporating new features and dependency upgrades, demonstrating active maintenance and evolution.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the main `is` object and individual type checkers, then uses them to inspect various values and show type guards in action.

import is from 'is-lite';
import { isString, isInteger } from 'is-lite/standalone';

function processValue(value: unknown) {
  // Using the default 'is' function to get type string
  console.log(`Type of value: ${is(value)}`);

  // Using a specific type checker from the default import
  if (is.string(value)) {
    console.log(`Value is a string: "${value}"`);
  }

  // Using a specific type checker from standalone imports
  if (isString(value)) {
    console.log(`Value is definitely a string via isString: "${value}"`);
  }

  // Demonstrate a new checker from v2.0.0
  if (isInteger(value)) {
    console.log(`Value is an integer: ${value}`);
  }

  // Example of is.arrayOf
  const mixedArray = ['a', 1, 'b'];
  const stringArray = ['hello', 'world'];
  console.log(`Is mixedArray all strings? ${is.arrayOf(mixedArray, is.string)}`);
  console.log(`Is stringArray all strings? ${is.arrayOf(stringArray, is.string)}`);
}

processValue('Hello, TypeScript');
processValue(42);
processValue(null);
processValue([1, 2, 3]);

view raw JSON →