npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chaisser/type-guard

v1.0.0

Published

Runtime type guards and validators

Downloads

66

Readme

🛡️ @chaisser/type-guard

Runtime type guards and validators with full TypeScript support


✨ Features

  • 🎯 Type-safe - Full TypeScript support with automatic type narrowing
  • 🧪 30+ guards - Comprehensive coverage of all JavaScript types
  • Assertions - Throw-on-failure assertions with TypeGuardError
  • 🔍 Structural checks - Plain objects, promises, maps, sets, typed arrays
  • 📏 Number precision - Finite, integer, safe integer, NaN detection
  • 🧹 Emptiness checks - Works on strings, arrays, objects, maps, sets
  • 🪶 Zero dependencies - Lightweight and tree-shakeable
  • 🏎️ ESM + CJS - Dual module format support

📦 Installation

npm install @chaisser/type-guard
# or
yarn add @chaisser/type-guard
# or
pnpm add @chaisser/type-guard

🚀 Quick Start

import {
  // Primitive guards
  isString,
  isNumber,
  isBoolean,
  isNull,
  isUndefined,
  isNil,

  // Structural guards
  isPlainObject,
  isArray,
  isDate,
  isPromise,

  // Emptiness
  isEmpty,
  isBlank,

  // Assertions
  assertString,
  assertArray,
  assertNonNil,
} from '@chaisser/type-guard';

// Type guards narrow types automatically
function process(value: unknown) {
  if (isString(value)) {
    console.log(value.toUpperCase()); // TypeScript knows it's string
  }
}

// Assertions throw TypeGuardError on mismatch
function greet(name: unknown) {
  assertString(name);
  return `Hello, ${name}`;
}

// Check emptiness for any container
isEmpty(null);      // true
isEmpty('');        // true
isEmpty([]);        // true
isEmpty({});        // true
isEmpty(new Map()); // true
isEmpty('hello');   // false

📖 What It Does

This package provides runtime type checking utilities that integrate seamlessly with TypeScript's type system. Every guard function acts as a type predicate, so TypeScript automatically narrows types in conditional branches. Assertion functions provide a throw-based alternative for validating inputs at function boundaries.


🎯 How It Works

The package is organized into categories:

  • Primitive Guards - Check for basic types (string, number, boolean, symbol, bigint, null, undefined)
  • Number Guards - Precision checks (finite, integer, safe integer, NaN)
  • Structural Guards - Object types (plain object, array, date, regex, error, promise, map, set, typed array)
  • Emptiness Guards - Check if containers are empty (string, array, object, map, set)
  • Assertions - Throw TypeGuardError instead of returning boolean

🎨 What It's Useful For

  • API Input Validation - Validate incoming request data at runtime
  • Type Narrowing - Help TypeScript understand unknown values in conditional branches
  • Defensive Programming - Guard against unexpected data shapes
  • Form Validation - Check user input types before processing
  • Configuration Parsing - Validate parsed JSON/yaml config structures
  • Library Authoring - Provide clear error messages for invalid arguments
  • Data Sanitization - Filter and validate data from external sources

💡 Usage Examples

Primitive Checks

import { isString, isNumber, isBoolean, isNil, isPrimitive } from '@chaisser/type-guard';

isString('hello');     // true
isString(42);          // false

isNumber(42);          // true
isNumber(NaN);         // false (NaN is excluded)
isNumber('42');        // false

isBoolean(true);       // true
isBoolean(0);          // false

isNil(null);           // true
isNil(undefined);      // true
isNil(0);              // false

isPrimitive('hi');     // true
isPrimitive({});       // false

Number Precision

import { isFiniteNumber, isInteger, isSafeInteger, isNaNValue } from '@chaisser/type-guard';

isFiniteNumber(42);      // true
isFiniteNumber(Infinity); // false
isFiniteNumber(NaN);     // false

isInteger(7);            // true
isInteger(3.14);         // false

isSafeInteger(42);                  // true
isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false

isNaNValue(NaN);         // true
isNaNValue(42);          // false

Structural Checks

import {
  isPlainObject, isArray, isDate, isRegExp,
  isError, isPromise, isMap, isSet, isTypedArray
} from '@chaisser/type-guard';

isPlainObject({});                    // true
isPlainObject(Object.create(null));   // true
isPlainObject(new Date());            // false

isArray([1, 2, 3]);                   // true
isArray({ length: 3 });               // false

isDate(new Date());                   // true
isDate(new Date('invalid'));          // false (invalid date)

isPromise(Promise.resolve(42));       // true
isPromise({ then: () => {} });        // true (thenable)

isMap(new Map());                     // true
isSet(new Set([1, 2]));               // true
isTypedArray(new Uint8Array(4));      // true

Emptiness Checks

import { isEmpty, isBlank } from '@chaisser/type-guard';

// Works across all container types
isEmpty(null);          // true
isEmpty(undefined);     // true
isEmpty('');            // true
isEmpty([]);            // true
isEmpty({});            // true
isEmpty(new Map());     // true
isEmpty(new Set());     // true

isEmpty('hello');       // false
isEmpty([1]);           // false
isEmpty({ a: 1 });     // false

// String-specific blank check
isBlank('');            // true
isBlank('   ');         // true
isBlank('\t\n');        // true
isBlank('hello');       // false

Assertions

import {
  assertString, assertNumber, assertArray,
  assertPlainObject, assertNonNil, assertDate,
  TypeGuardError
} from '@chaisser/type-guard';

// Passes silently when correct
assertString('hello');
assertNumber(42);
assertArray([1, 2, 3]);

// Throws TypeGuardError when wrong
try {
  assertString(42);
} catch (e) {
  console.log(e instanceof TypeGuardError); // true
  console.log(e.message); // "Expected string, got number"
}

// Great for function boundaries
function processUser(data: unknown) {
  assertPlainObject(data);
  assertNonNil(data.name);
  // TypeScript now knows data is Record<string, unknown>
}

📚 API Reference

Primitive Guards

| Function | Type Predicate | Description | |---|---|---| | isString(value) | value is string | Checks if value is a string | | isNumber(value) | value is number | Checks if value is a number (excludes NaN) | | isBoolean(value) | value is boolean | Checks if value is a boolean | | isSymbol(value) | value is symbol | Checks if value is a symbol | | isBigInt(value) | value is bigint | Checks if value is a bigint | | isNull(value) | value is null | Checks if value is null | | isUndefined(value) | value is undefined | Checks if value is undefined | | isNil(value) | value is null \| undefined | Checks if value is null or undefined | | isPrimitive(value) | value is primitive | Checks if value is a primitive type |

Number Guards

| Function | Type Predicate | Description | |---|---|---| | isFiniteNumber(value) | value is number | Finite number (excludes NaN, Infinity) | | isInteger(value) | value is number | Integer number | | isSafeInteger(value) | value is number | Safe integer (within MAX_SAFE_INTEGER) | | isNaNValue(value) | boolean | Checks if value is NaN |

Structural Guards

| Function | Type Predicate | Description | |---|---|---| | isObject(value) | value is object | Any non-null object | | isPlainObject(value) | value is Record<string, unknown> | Plain object ({} or Object.create(null)) | | isFunction(value) | value is Function | Any function or class | | isArray(value) | value is unknown[] | An array | | isDate(value) | value is Date | Valid Date instance | | isRegExp(value) | value is RegExp | RegExp instance | | isError(value) | value is Error | Error instance | | isPromise(value) | value is Promise<T> | Promise or thenable object | | isMap(value) | value is Map<K, V> | Map instance | | isSet(value) | value is Set<T> | Set instance | | isTypedArray(value) | boolean | Any typed array |

Emptiness Guards

| Function | Returns | Description | |---|---|---| | isEmpty(value) | boolean | Empty container (null, "", [], {}, empty Map/Set) | | isBlank(value) | boolean | Empty or whitespace-only string |

Assertions

Each assertion throws TypeGuardError if the check fails.

| Function | Asserts | |---|---| | assertString(value) | Value is a string | | assertNumber(value) | Value is a number (not NaN) | | assertBoolean(value) | Value is a boolean | | assertFunction(value) | Value is a function | | assertArray(value) | Value is an Array | | assertPlainObject(value) | Value is a plain Object | | assertNonNil(value) | Value is neither null nor undefined | | assertDate(value) | Value is a valid Date |


🔗 Related Packages

Explore our other utility packages in the @chaisser namespace:


🔒 License

MIT - Free to use in personal and commercial projects


👨 Developed by

Doruk Karaboncuk [email protected]


📄 Repository


🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

📞 Support

For issues, questions, or suggestions, please reach out through:


Made with ❤️ by @chaisser

npm license downloads typescript