Typical Type Checking Utilities

7.3.0 · active · verified Sun Apr 19

Typical is an isomorphic, functional JavaScript library providing comprehensive type-checking utilities. Currently at version 7.3.0, it offers a stable and reliable API for validating values against common types. Key differentiators include its precise handling of numbers (distinguishing `NaN` from valid numbers, and providing checks for finite numbers), its ability to differentiate between plain object literals and other object instances, and its functional approach, making it ideal for use in array methods like `every` or `filter`. The library maintains a steady release cadence focused on stability and compatibility with modern JavaScript environments. It aims to be a foundational utility for robust data validation in both Node.js and browser contexts.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the default `t` object and using various type-checking functions like `isNumber`, `isFiniteNumber`, `isPlainObject`, `isObject`, `isDefined`, `isUndefined`, `isString`, and `isArrayLike` on different data types.

import t from 'typical';

const data = [
  10, NaN, Infinity, 'hello', null, undefined,
  { a: 1 }, new Date(), [1, 2, 3]
];

console.log('--- Checking Numbers ---');
console.log('Is 10 a number?', t.isNumber(10)); // true
console.log('Is NaN a number?', t.isNumber(NaN)); // false (differentiator from typeof)
console.log('Is Infinity a finite number?', t.isFiniteNumber(Infinity)); // false

console.log('\n--- Checking Objects ---');
console.log('Is { a: 1 } a plain object?', t.isPlainObject({ a: 1 })); // true
console.log('Is new Date() a plain object?', t.isPlainObject(new Date())); // false
console.log('Is new Date() an object?', t.isObject(new Date())); // true

console.log('\n--- General Checks ---');
console.log('All defined values in data:', data.filter(t.isDefined));
console.log('Any undefined values in data:', data.some(t.isUndefined));
console.log('Are all strings?', ['a', 'b', 'c'].every(t.isString)); // true
console.log('Is an array-like object?', t.isArrayLike({ length: 3, 0: 'a' })); // true

view raw JSON →