Type System

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 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.

Installation

npm i -D @halvaradop/ts-utility-types

Usage

import type { , ,  } from "@halvaradop/ts-utility-types/type-guards"
import type * as  from "@halvaradop/ts-utility-types/type-guards"


type  = .
  • AnyOf
  • Equals
  • Expect
  • IsAny
  • IsArray
  • IsFunction
  • IsNever
  • IsObject
  • Not

Equals<X, Y>

ArgumentTypeDescription
XunknownThe first type to compare.
YunknownThe second type to compare.

Checks if two types are strictly equal.

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

type CheckTrue = <true, true>
type CheckTrue = true
type CheckFalse = <() => {}, true>
type CheckFalse = false

Expect<T>

ArgumentTypeDescription
TtrueThe type to check for being true.

Ensures the received type is true. Useful for type-level tests.

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

type CheckTrue = <true>
type CheckTrue = true
type = <false>
Type 'false' does not satisfy the constraint 'true'.

Not<T>

ArgumentTypeDescription
TbooleanThe boolean type to negate.

Negates a boolean type value.

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

type CheckFalse = <true>
type CheckFalse = false
type CheckTrue = <false>
type CheckTrue = true

IsNever<T>

ArgumentTypeDescription
TunknownThe type to check for never.

Checks if the type is never.

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

type CheckNever = <never>
type CheckNever = true
type CheckStr = <string>
type CheckStr = false

IsAny<T>

ArgumentTypeDescription
TunknownThe type to check for any.

Checks if the type is any.

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

type CheckAny = <any>
type CheckAny = true
type CheckStr = <string>
type CheckStr = false

AnyOf<T>

ArgumentTypeDescription
Treadonly any[]The tuple to check for any truthy value.

Checks if any value in the tuple is truthy.

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

type Test1 = <[0, "", false, [], {}, undefined, null, true]>
type Test1 = true
type Test2 = <[0, "", false, [], {}, undefined, null]>
type Test2 = false

IsArray<T>

Checks if the type is an array.

ArgumentTypeDescription
TunknownThe type to check for being an array.
import type {  } from "@halvaradop/ts-utility-types/type-guards"

type Test1 = <[1, 2, 3]>
type Test1 = true
type Test2 = <{ : string }>
type Test2 = false

IsObject<T>

Checks if the type is an object (excluding arrays and functions).

ArgumentTypeDescription
TunknownThe type to check for being an object.
import type {  } from "@halvaradop/ts-utility-types/type-guards"

type Test1 = <{ : string }>
type Test1 = true
type Test2 = <[1, 2, 3]>
type Test2 = false

IsFunction<T>

Checks if the type is a function.

ArgumentTypeDescription
TunknownThe type to check for being a function.
import type {  } from "@halvaradop/ts-utility-types/type-guards"

type Test1 = <() => void>
type Test1 = true
type Test2 = <{ : string }>
type Test2 = false