Runtime Validation
Helpers and type guards for runtime value validation in TypeScript and JavaScript. These functions check if a value is primitive, nullish, boolean, string, number, object, array, function, or falsy.
Runtime Validation
Helpers and type guards for runtime value validation in TypeScript and JavaScript. These functions check if a value is primitive, nullish, boolean, string, number, object, array, function, or falsy.
Runtime Functions
Unlike other modules, these are runtime functions that can be used in JavaScript and TypeScript for actual value validation with proper type guards. This package should be installed as a production package for access to the validators in the build and final app
Installation
npm i @halvaradop/ts-utility-types
Usage
import { isPrimitive, isNullish, isArray } from "@halvaradop/ts-utility-types/validate"
import * as validate from "@halvaradop/ts-utility-types/validate"
validate.- isArray
- isBoolean
- isFalsy
- isFunction
- isNullish
- isNumber
- isObject
- isPrimitive
- isPrimitiveNullish
- isString
Type Guards
isPrimitive(value: unknown): value is Primitive
Checks if a value is a primitive type (number
, string
, boolean
, bigint
, symbol
).
import { isPrimitive } from "@halvaradop/ts-utility-types/validate"
const num = isPrimitive(42)
const str = isPrimitive("hello")
const obj = isPrimitive({})
isPrimitiveNullish(value: unknown): value is PrimitiveNullish
Checks if a value is a primitive or nullish (null
or undefined
).
import { isPrimitiveNullish } from "@halvaradop/ts-utility-types/validate"
const und = isPrimitiveNullish(undefined)
const str = isPrimitiveNullish("hello")
const obj = isPrimitiveNullish({})
isNullish(value: unknown): value is Nullish
Checks if a value is null
or undefined
.
import { isNullish } from "@halvaradop/ts-utility-types/validate"
const empty = isNullish(null)
const und = isNullish(undefined)
const num = isNullish(0)
isBoolean(value: unknown): value is boolean
Checks if a value is boolean.
import { isBoolean } from "@halvaradop/ts-utility-types/validate"
const bol = isBoolean(true)
const str = isBoolean("true")
isString(value: unknown): value is string
Checks if a value is a string.
import { isString } from "@halvaradop/ts-utility-types/validate"
const str = isString("hello")
const num = isString(123)
isNumber(value: unknown): value is number
Checks if a value is numeric (including NaN
).
import { isNumber } from "@halvaradop/ts-utility-types/validate"
const num = isNumber(42)
const str = isNumber("42")
isObject(value: unknown): value is object
Checks if a value is a plain object (not array, not function, not nullish).
import { isObject } from "@halvaradop/ts-utility-types/validate"
const obj = isObject({})
const arr = isObject([])
const empty = isObject(null)
isArray(value: unknown): value is unknown[]
Checks if a value is an array.
import { isArray } from "@halvaradop/ts-utility-types/validate"
const nums = isArray([1, 2, 3])
const obj = isArray({})
isFunction(value: unknown): value is Function
Checks if a value is a function.
import { isFunction } from "@halvaradop/ts-utility-types/validate"
const fn = isFunction(() => {})
const num = isFunction(123)
isFalsy(value: unknown): boolean
Checks if a value is falsy (null
, undefined
, false
, 0
, ""
).
import { isFalsy } from "@halvaradop/ts-utility-types/validate"
const num = isFalsy(0)
const str = isFalsy("hello")
Type Guards
Utilities for type checks and validations in TypeScript. These types help compare, validate, and discriminate types at compile time, enabling advanced conditional types and type guards.
Type-Level Testing
Utilities for type-level testing and assertions in TypeScript. These types help compare, validate, and ensure type conditions in testing or advanced development scenarios.