Type System

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 { , ,  } from "@halvaradop/ts-utility-types/validate"
import * as  from "@halvaradop/ts-utility-types/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 {  } from "@halvaradop/ts-utility-types/validate"

const num = (42)
const num: boolean
const str = ("hello")
const str: boolean
const obj = ({})
const obj: boolean

isPrimitiveNullish(value: unknown): value is PrimitiveNullish

Checks if a value is a primitive or nullish (null or undefined).

import {  } from "@halvaradop/ts-utility-types/validate"

const und = ()
const und: boolean
const str = ("hello")
const str: boolean
const obj = ({})
const obj: boolean

isNullish(value: unknown): value is Nullish

Checks if a value is null or undefined.

import {  } from "@halvaradop/ts-utility-types/validate"

const empty = (null)
const empty: boolean
const und = ()
const und: boolean
const num = (0)
const num: boolean

isBoolean(value: unknown): value is boolean

Checks if a value is boolean.

import {  } from "@halvaradop/ts-utility-types/validate"

const bol = (true)
const bol: boolean
const str = ("true")
const str: boolean

isString(value: unknown): value is string

Checks if a value is a string.

import {  } from "@halvaradop/ts-utility-types/validate"

const str = ("hello")
const str: boolean
const num = (123)
const num: boolean

isNumber(value: unknown): value is number

Checks if a value is numeric (including NaN).

import {  } from "@halvaradop/ts-utility-types/validate"

const num = (42)
const num: boolean
const str = ("42")
const str: boolean

isObject(value: unknown): value is object

Checks if a value is a plain object (not array, not function, not nullish).

import {  } from "@halvaradop/ts-utility-types/validate"

const obj = ({})
const obj: boolean
const arr = ([])
const arr: boolean
const empty = (null)
const empty: boolean

isArray(value: unknown): value is unknown[]

Checks if a value is an array.

import {  } from "@halvaradop/ts-utility-types/validate"

const nums = ([1, 2, 3])
const nums: boolean
const obj = ({})
const obj: boolean

isFunction(value: unknown): value is Function

Checks if a value is a function.

import {  } from "@halvaradop/ts-utility-types/validate"

const fn = (() => {})
const fn: boolean
const num = (123)
const num: boolean

isFalsy(value: unknown): boolean

Checks if a value is falsy (null, undefined, false, 0, "").

import {  } from "@halvaradop/ts-utility-types/validate"

const num = (0)
const num: boolean
const str = ("hello")
const str: boolean