Skip to main content

Predicates & Type Guards

Runtime Validation with Precision

NHB Toolbox provides two complementary validation tools:

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

Browse by category or use the search to find the perfect predicate/type guard for your task.