This is a TypeScript library that provides a set of utility functions for type guarding. Type guarding allows you to perform runtime checks on values to determine if they satisfy certain conditions. The library offers various functions to check and validate types, create literal type guards, validate arrays and tuples, and more.
npm install typeofit
string
object
number
undef
func
nul
literal<T extends any>(of: T)
array<T = any>(of?: Guard<T>)
tuple<T extends Guard<any>[]>(...of: T)
record<K extends string = string, V = any>(key?: Guard<K>, value?: Guard<V>)
struct<T extends Record<any, Guard<any>>>(of: T, strict: boolean = true)
defined<T>(x: T | undefined | null): x is T
or<T extends Guard<any>[]>(...of: T)
is<T extends V, V = unknown>(cb: (x: V) => boolean): Guard<T, V>
Guard<T extends V, V = unknown>
TypeOfGuard<G>
Install the library using npm or yarn:
npm install my-type-guards-library
import { tp } from 'typeofit'
const unknown: unknown;
if (tp.string(unknown)) // string
if (tp.number(unknown)) // number
if (tp.array()(unknown)) // unknown[]
if (tp.array(tp.string)(unknown)) // string[]
if (tp.record()(unknown)) // Record<string, any>
if (tp.record(tp.literal('foo'), tp.number)(unknown)) // { foo: number }
if (tp.tuple(tp.literal('foo'), tp.literal(10))(unknown)) // ['foo', 10]
if (tp.struct({ foo: tp.literal('bar') })(unknown)) // { foo: 'bar' }
if (tp.or(tp.literal('foo'), tp.literal('bar'))(unknown)) // 'foo' | 'bar'
const items: (number | null)[]
const defined = items.filter(tp.defined) // number[]
A type guard function that checks if a value is of type string.
A type guard function that checks if a value is of type object.
A type guard function that checks if a value is of type number.
A type guard function that checks if a value is undefined.
A type guard function that checks if a value is of type Function.
A type guard function that checks if a value is null.
The literal
function creates a type guard that checks if a value is equal to a specific literal value.
The array
function checks if a value is an array and optionally checks if all elements of the array match a given type guard.
The tuple
function checks if a value is an array with elements that match the specified guards.
The record
function checks if a given value is a valid record object, with optional key and value guards.
The struct
function is a type guard that checks if an object matches a given structure defined by a record of guards.
The defined
function checks if a value is defined (not undefined
or null
) and returns a boolean.
The or
function allows you to check if a value matches any of the provided guards.
The is
function is a TypeScript type guard that checks if a value satisfies a given condition. It takes a callback function cb
that takes a value of type V
and returns a boolean value. The callback function is used to define the condition for type guarding.
A type alias representing a TypeScript type guard function. It takes a value of type V
and returns a boolean indicating whether the value is of type T
.
A conditional type that extracts the guarded type from a Guard
function.
MIT © Dan Beaven