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 { Equals, IsNever, IsAny } from "@halvaradop/ts-utility-types/type-guards"
import type * as Guards from "@halvaradop/ts-utility-types/type-guards"
type Utilities = Guards.- AnyOf
- Equals
- Expect
- IsAny
- IsArray
- IsFunction
- IsNever
- IsObject
- Not
Equals<X, Y>
Argument | Type | Description |
---|---|---|
X | unknown | The first type to compare. |
Y | unknown | The second type to compare. |
Checks if two types are strictly equal.
import type { Equals } from "@halvaradop/ts-utility-types/type-guards"
type CheckTrue = Equals<true, true>
type CheckFalse = Equals<() => {}, true>
Expect<T>
Argument | Type | Description |
---|---|---|
T | true | The type to check for being true. |
Ensures the received type is true
. Useful for type-level tests.
import type { Expect } from "@halvaradop/ts-utility-types/type-guards"
type CheckTrue = Expect<true>
type CheckFalse = Expect<false>
Not<T>
Argument | Type | Description |
---|---|---|
T | boolean | The boolean type to negate. |
Negates a boolean type value.
import type { Not } from "@halvaradop/ts-utility-types/type-guards"
type CheckFalse = Not<true>
type CheckTrue = Not<false>
IsNever<T>
Argument | Type | Description |
---|---|---|
T | unknown | The type to check for never. |
Checks if the type is never
.
import type { IsNever } from "@halvaradop/ts-utility-types/type-guards"
type CheckNever = IsNever<never>
type CheckStr = IsNever<string>
IsAny<T>
Argument | Type | Description |
---|---|---|
T | unknown | The type to check for any. |
Checks if the type is any
.
import type { IsAny } from "@halvaradop/ts-utility-types/type-guards"
type CheckAny = IsAny<any>
type CheckStr = IsAny<string>
AnyOf<T>
Argument | Type | Description |
---|---|---|
T | readonly any[] | The tuple to check for any truthy value. |
Checks if any value in the tuple is truthy.
import type { AnyOf } from "@halvaradop/ts-utility-types/type-guards"
type Test1 = AnyOf<[0, "", false, [], {}, undefined, null, true]>
type Test2 = AnyOf<[0, "", false, [], {}, undefined, null]>
IsArray<T>
Checks if the type is an array.
Argument | Type | Description |
---|---|---|
T | unknown | The type to check for being an array. |
import type { IsArray } from "@halvaradop/ts-utility-types/type-guards"
type Test1 = IsArray<[1, 2, 3]>
type Test2 = IsArray<{ key: string }>
IsObject<T>
Checks if the type is an object (excluding arrays and functions).
Argument | Type | Description |
---|---|---|
T | unknown | The type to check for being an object. |
import type { IsObject } from "@halvaradop/ts-utility-types/type-guards"
type Test1 = IsObject<{ key: string }>
type Test2 = IsObject<[1, 2, 3]>
IsFunction<T>
Checks if the type is a function.
Argument | Type | Description |
---|---|---|
T | unknown | The type to check for being a function. |
import type { IsFunction } from "@halvaradop/ts-utility-types/type-guards"
type Test1 = IsFunction<() => void>
type Test2 = IsFunction<{ key: string }>
Deep Utilities
Advanced utilities for deep manipulation of objects and nested types in TypeScript. These types allow you to merge, transform, filter, select, omit, and modify properties at any depth in a type-safe and expressive way.
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.