Typical Type Checking Utilities
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
-
TypeError: (0 , typical__WEBPACK_IMPORTED_MODULE_0__.isNumber) is not a function
cause Attempting to named import individual functions directly from 'typical' in an ESM context, while the package exports them as properties of a default object.fixCorrect the import to `import t from 'typical'` and access functions as `t.isNumber` or destructure like `const { isNumber } = t;`. -
TypeError: Cannot read properties of undefined (reading 'isNumber')
cause The default export `t` was not correctly imported or `require()`d, leading to `t` being `undefined` or an empty object, and thus `t.isNumber` doesn't exist.fixEnsure `import t from 'typical'` (ESM) or `const t = require('typical')` (CJS) is at the top of your file and executed correctly. Check for typos in the import path or variable name. -
ERR_REQUIRE_ESM
cause Attempting to `require()` the `typical` package in a CommonJS module when the package is designed as ESM-only or explicitly specifies `"type": "module"` without a CJS entry point for `require`.fixFor CommonJS projects, either configure your bundler/environment to transpile ESM to CJS, or explicitly use dynamic `import('typical').then(m => m.default)` if it's ESM-only. The best fix is to migrate to ESM (`"type": "module"` in `package.json`) and use `import t from 'typical'`.
Warnings
- breaking Older Node.js environments (pre-12.17) might encounter issues with module resolution or performance. While the package specifies `>=12.17`, consistent ESM/CJS interop can be complex in mixed environments. Users should ensure their Node.js version meets or exceeds the minimum requirement for full compatibility.
- gotcha The `isNumber` function in `typical` returns `false` for `NaN`, which differs from JavaScript's built-in `typeof NaN === 'number'`. If you need to include `NaN` as a number, or specifically exclude `Infinity`, use `isFiniteNumber` or `Number.isFinite` directly, or combine `t.isNumber` with `Number.isNaN` for custom logic.
- gotcha The `isPlainObject` method specifically checks for objects created by the `Object` constructor or object literals. It returns `false` for instances of custom classes (e.g., `new MyClass()`), Arrays, or Dates. Use `isObject` if you simply need to check if a value is any object (not `null`).
- gotcha When using `require()` in CommonJS modules, if the `package.json` of `typical` is configured for ESM with a default export, you might implicitly need to access the `.default` property (e.g., `require('typical').default`). However, modern Node.js often handles interop automatically, so this is a 'depends on environment' situation.
Install
-
npm install typical -
yarn add typical -
pnpm add typical
Imports
- t
import { isNumber, isPlainObject } from 'typical'import t from 'typical'
- t
const t = require('typical').defaultconst t = require('typical') - isNumber
import { isNumber } from 'typical'import t from 'typical'; const isNum = t.isNumber;
Quickstart
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