Typpy: Enhanced Type Checking
Typpy is a lightweight JavaScript utility that provides a more robust and accurate approach to determining variable types compared to the native `typeof` operator. It addresses common ambiguities of `typeof`, such as `null` evaluating to "object" and distinguishing between different object types like arrays and plain objects. The library allows for type comparison against both string representations (e.g., "array") and constructor functions (e.g., `Array`). Currently at version 2.4.0, its release cadence is moderate, primarily consisting of minor updates for documentation, sponsor links, and small bug fixes like handling null/undefined constructors. It differentiates itself by offering a consolidated API for precise type checks, simplifying conditional logic and reducing potential type-related bugs in applications.
Common errors
-
TypeError: Typpy.is is not a function
cause Attempting to import `is` as a named export (`import { is } from 'typpy';`) instead of accessing it as a property of the default export.fixChange your import statement to `import Typpy from 'typpy';` and then call `Typpy.is(value, type);`. -
ReferenceError: require is not defined
cause Using `const Typpy = require('typpy');` in an environment configured for ECMAScript Modules (ESM) without a CommonJS shim.fixUpdate your import statement to use ESM syntax: `import Typpy from 'typpy';`. -
Typpy is not defined
cause The Typpy package was not correctly installed or imported. This could be due to a typo in the import path or a missing `npm install typpy` step.fixFirst, ensure the package is installed: `npm install typpy`. Then, verify the import statement `import Typpy from 'typpy';` (for ESM) or `const Typpy = require('typpy');` (for CommonJS) is correct and at the top of your file.
Warnings
- gotcha The documentation primarily showcases CommonJS `require` syntax. While modern Node.js and bundlers support `import Typpy from 'typpy';` for ESM, direct named exports like `import { is } from 'typpy';` are not provided. Always import the default `Typpy` object and access `is` and `get` as properties.
- gotcha Typpy's primary function `Typpy(input, target)` returns a boolean if `target` is provided, but a string if `target` is omitted. Ensure you handle both return types correctly in your logic.
- gotcha Unlike the native `typeof` operator which returns 'object' for `null`, `Typpy.is(null, 'null')` correctly evaluates to `true`. Be aware of this difference when migrating code or expecting native `typeof` behavior.
Install
-
npm install typpy -
yarn add typpy -
pnpm add typpy
Imports
- Typpy
const Typpy = require('typpy');import Typpy from 'typpy';
- Typpy.is
import { is } from 'typpy';import Typpy from 'typpy'; Typpy.is(value, type);
- Typpy.get
import { get } from 'typpy';import Typpy from 'typpy'; Typpy.get(value);
Quickstart
import Typpy from 'typpy';
console.log(Typpy(0));
// => "number"
console.log(Typpy('', String));
// => true
console.log(Typpy.is(null, 'null'));
// => true
console.log(Typpy.get([]));
// => Array
console.log(Typpy({}, true));
// => false
console.log(Typpy({}, Object));
// => true
console.log(Typpy.get({}));
// => Object
console.log(Typpy.get(42, true));
// => "number"