JavaScript Type Checking Utilities
`is-what` is a lightweight, fully TypeScript-supported utility library providing a comprehensive set of functions for JavaScript type checking, such as `isPlainObject()`, `isArray()`, `isString()`, and many more specific checks. Currently at version `5.5.0`, the library maintains a steady release cadence, frequently adding new type-checking utilities and dependency updates. Its core differentiators include a focus on simplicity, a small footprint, robust TypeScript type inference (automatically narrowing types after checks), and a more intuitive handling of edge cases like `NaN` compared to native JavaScript functions. It explicitly distinguishes between plain objects (`{}`) and class instances, addressing a common pain point in JavaScript development. The library aims to be a straightforward and reliable alternative to more complex or less type-safe existing solutions. Its `v5.x` releases moved to ESM-only and refined its TypeScript integration.
Common errors
-
TypeError [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/is-what/dist/index.js from ... not supported.
cause Attempting to use `require()` with `is-what` v5 and later, which is an ESM-only package.fixChange `const { someFn } = require('is-what')` to `import { someFn } from 'is-what'`. -
Property 'xyz' does not exist on type 'unknown'.
cause After checking a variable with `is-what`, TypeScript still infers it as `unknown` due to the v5 update, preventing direct property access.fixExplicitly narrow the type using a type assertion `(myVar as MyType).xyz` or perform further checks that TypeScript can use for inference. -
console.log(isNumber(NaN)); // Expected true, got false
cause `isNumber(NaN)` intentionally returns `false` in `is-what`.fixIf you need to check specifically if a value is `NaN`, use `isNaNValue(value)` instead.
Warnings
- breaking `is-what` became an ESM-only package in version 5.0.0. CommonJS `require()` statements will no longer work.
- breaking In `v5.0.0`, type definitions were updated to use `unknown` instead of `any` for better type safety. This might cause TypeScript errors if your code relied on `any` for inferred types.
- gotcha The `isNumber()` function in `is-what` explicitly returns `false` for `NaN`, differing from JavaScript's native `typeof NaN === 'number'` or `Number.isNaN()` behavior. Use `isNaNValue()` for specific `NaN` checks.
- security Version `5.1.0` included a fix for a security issue identified via `npm audit`. Users on older `v5.x` versions should upgrade.
- gotcha The `isHexDecimal` function, introduced in `v5.2.0`, accepts an optional second argument for checking a specific string length, useful for IDs like MongoDB ObjectIds. Omitting it will only check for valid hexadecimal characters.
Install
-
npm install is-what -
yarn add is-what -
pnpm add is-what
Imports
- isPlainObject
const { isPlainObject } = require('is-what')import { isPlainObject } from 'is-what' - isString
import isString from 'is-what'
import { isString } from 'is-what' - isNaNValue
import { isNaNValue } from 'is-what'
Quickstart
import { isString, isNumber, isPlainObject, isArray, isDate, isNaNValue, isHexDecimal } from 'is-what';
console.log('Is "hello" a string?', isString('hello')); // true
console.log('Is 123 a number?', isNumber(123)); // true
console.log('Is NaN a number (is-what)?', isNumber(NaN)); // false, by design
console.log('Is NaN truly NaN?', isNaNValue(NaN)); // true
console.log('Is {} a plain object?', isPlainObject({})); // true
console.log('Is new Date() a plain object?', isPlainObject(new Date())); // false
console.log('Is [] an array?', isArray([])); // true
console.log('Is new Date() a Date object?', isDate(new Date())); // true
console.log('Is an invalid date a Date object?', isDate(new Date('invalid date'))); // false
console.log('Is "60adf084f0fbdcab42de841e" a hex decimal of length 24?', isHexDecimal('60adf084f0fbdcab42de841e', 24)); // true