is-lite: Lightweight Type Checking
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
-
TypeError: (0 , is_lite__WEBPACK_IMPORTED_MODULE_0__.isString) is not a function
cause Attempting to import `isString` or other individual checkers directly from `is-lite` instead of `is-lite/standalone` in an ESM context, or a bundler incorrectly resolving module paths.fixChange your import statement from `import { isString } from 'is-lite';` to `import { isString } from 'is-lite/standalone';`. -
TypeError: is.isString is not a function
cause Attempting to access `isString` as a direct property of the default `is` object, while `isString` is intended for standalone import or direct call if `is` itself is a function.fixIf you imported `is` as the main object, access specific checkers as `is.string(value)`. If you want the tree-shakeable `isString` function, import it directly via `import { isString } from 'is-lite/standalone';`.
Warnings
- breaking Version 2.0.0 introduced separate bundles for ESM and CommonJS. If you are using the `import` syntax, your bundler must correctly support ESM resolution for `is-lite`. Old CJS-only bundler configurations might break.
- breaking Prior to v1.2.0, the package's export structure was different. Specifically, named exports were not consistently available or structured as they are in current versions. Updates in v1.1.0 and v1.2.0 refined these exports.
- gotcha The `is.number()` function will return `false` for `NaN` (Not-a-Number). If you intend to specifically check for valid numeric values excluding `NaN`, this is the desired behavior. If `NaN` should be considered a number for your use case, use `is.nan()` in conjunction or check `typeof value === 'number'`.
- gotcha The `is.instanceOf(value, class)` checker only validates if `value` is a *direct* instance of the specified `class`. It does not check if `value` is an instance of a superclass in the prototype chain.
Install
-
npm install is-lite -
yarn add is-lite -
pnpm add is-lite
Imports
- is
const is = require('is-lite');import is from 'is-lite';
- isString
import { isString } from 'is-lite'; const { isString } = require('is-lite');import { isString } from 'is-lite/standalone'; - isArrayOf
import { isArrayOf } from 'is-lite';
Quickstart
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]);