Toolbox-XToolbox-X
GuardsPrimitive Type Guards

Core Primitive Type Guards

Runtime type checking of core primitive values.

Collection of type-guards for runtime type checking of core primitive values, enabling TypeScript type narrowing.

Imports

import { isNumber, isString, isBoolean, isSymbol, isBigint } from 'toolbox-x/guards';

isNumber

Validates whether a value is a finite number and not NaN.

This function returns true only if:

  • the value is of type number (not a string, object, etc.), and
  • the value is not NaN, Infinity, or -Infinity.

This ensures strict numeric validation suitable for mathematical operations, user input sanitization, or database-safe values.

Signature

isNumber(value: unknown): value is number

Examples

playground.ts

Type Narrowing Example:

const input: unknown = 10;

if (isNumber(input)) {
  input.toFixed(2);     // ✅ OK - input is treated as number
} else {
  // input is still unknown here
}

Info

Please, refer to numeric string guard: isNumericString for stringified number checking.


isString

Validates if a value is a string.

Signature

isString(value: unknown): value is string

Examples

playground.ts

isBoolean

Validates if a value is a boolean.

Signature

isBoolean(value: unknown): value is boolean

Examples

playground.ts

isSymbol

Validates if a value is a Symbol.

Signature

isSymbol(value: unknown): value is symbol

Examples

playground.ts

Key Notes

  1. Returns true only for actual Symbol primitives
  2. Useful for working with unique identifier patterns
  3. Helps maintain type safety when using Symbol properties

isBigInt

Checks if a value is a BigInt primitive. Essential for working with large integers.

Signature

isBigInt(value: unknown): value is bigint

Examples

playground.ts

Use Cases

  • Safe operations on large integers

Last updated: Sat, Jun 06, 2026 09:29:26AM (UTC)

On this page