@halvaradop/ts-utility-types/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.

import type * as Guards from "@halvaradop/ts-utility-types/type-guards"

Table of Contents

Equals<X, Y>

Checks if two types are strictly equal.

// Expected: true
type CheckTrue = Equals<true, true>

// Expected: false
type CheckFalse = Equals<() => {}, true>

Expect<T extends true>

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

// Expected: true
type CheckTrue = Expect<true>

// Expected: false (type error)
type CheckFalse = Expect<false>

Not<T extends boolean>

Negates a boolean type value.

// Expected: false
type CheckFalse = Not<true>

// Expected: true
type CheckTrue = Not<false>

IsNever<T>

Checks if the type is never.

// Expected: true
type CheckNever = IsNever<never>

// Expected: false
type CheckStr = IsNever<string>

IsAny<T>

Checks if the type is any.

// Expected: true
type CheckAny = IsAny<any>

// Expected: false
type CheckStr = IsAny<string>

AnyOf<T extends readonly any[]>

Checks if any value in the tuple is truthy.

// Expected: true
type Test1 = AnyOf<[0, "", false, [], {}, undefined, null, true]>

// Expected: false
type Test2 = AnyOf<[0, "", false, [], {}, undefined, null]>

IsArray<T>

Checks if the type is an array.

// Expected: true
type Test1 = IsArray<[1, 2, 3]>

// Expected: false
type Test2 = IsArray<{ key: string }>

IsObject<T>

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

// Expected: true
type Test1 = IsObject<{ key: string }>

// Expected: false
type Test2 = IsObject<[1, 2, 3]>

IsFunction<T>

Checks if the type is a function.

// Expected: true
type Test1 = IsFunction<() => void>

// Expected: false
type Test2 = IsFunction<{ key: string }>