Toolbox-XToolbox-X

Guards

Predicates & Type Guards — Toolbox-X Documentation.

Overview

toolbox-x provides a comprehensive set of type guards and predicate functions using the subpath 'toolbox-x/guards'. These guards help you safely validate and handle different types and shapes in your code.

1. Predicate Functions (Boolean Validators)

Simple functions that return boolean without affecting TypeScript's type system:

const prime = isPrime(number); // Returns true/false

2. Type Guards (Type Narrowing)

Functions using x is T syntax that narrow types in conditional blocks:

if (isString(input)) { // input is now narrowed to string type
  input.toLowerCase(); // Type-safe access
}

Key Differences

FeaturePredicatesType Guards
Return typebooleanvalue is Type
Type narrowingNoYes
UsageAnywhereConditionals
ExampleisPrime(value)isString(value)

Why This Matters

  • Predicates for simple validation
  • Guards when you need type narrowing

Imports

// All type guards and predicate functions can be imported using the following pattern:
import { guardName } from 'toolbox-x/guards';

// For example:
import { isArray, isObject, isString } from 'toolbox-x/guards';

Browse by category or use the search (ctrl+k) to find the perfect predicate/type guard for your task.

Last updated: Mon, May 25, 2026 06:06:17PM (UTC)

On this page