Typpy: Enhanced Type Checking

2.4.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the main `Typpy` function to get a type string or compare against a target, and how to use the `Typpy.is` and `Typpy.get` helper methods for specific type checks.

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"

view raw JSON →