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 numberExamples
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 stringExamples
playground.ts
isBoolean
Validates if a value is a boolean.
Signature
isBoolean(value: unknown): value is booleanExamples
playground.ts
isSymbol
Validates if a value is a Symbol.
Signature
isSymbol(value: unknown): value is symbolExamples
playground.ts
Key Notes
- Returns true only for actual Symbol primitives
- Useful for working with unique identifier patterns
- 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 bigintExamples
playground.ts
Use Cases
- Safe operations on large integers
Last updated: Sat, Jun 06, 2026 09:29:26AM (UTC)
