Isntnt is a collection of composable JavaScript runtime type predicates with TypeScript type guard declarations. Supports generics including union and intersection types.
Generics
above
(floor: number) => (value: unknown) => value is number
const isAboveZero = above(0)
and
<T extends Array<Predicate<any>>>(...predicates: T) => (value: unknown) => value is Predicate<Intersect<Static<T[number]>>>
const isBetween0And21 = and(above(0), below(21))
const isUser = shape({ name: isString })
const hasEmailAddress = at('email', isString)
const isUserWithEmail = and(isUser, hasEmailAddress)
array
<T>(predicate: Predicate<T>) => (value: unknown) => value is Array<T>
const isAnyArray = array(isAny)
at
<T extends PropertyKey, U>(key: T, predicate: Predicate<U>) => (value: unknown) => value is { [P in T]: U }
const isAnyAtFoo = at('foo', isAny)
below
(max: number) => (value: unknown) => value is number
const isBelow21 = below(21)
either
<T extends Array<Primitive>>(...literalValues: T) => (value: unknown) => value is T[number]
const isFooOrBar = either('foo', 'bar')
has
<T extends PropertyKey>(key: T) => (value: unknown) => value is { [P in T]: unknown }
const hasFoo = has('foo')
instance
<T extends Constructor<any, any>>(constructor: T) => (value: unknown) => value is InstanceType<T>
const isInstanceofString = instance(String)
literal
<T extends Primitive>(literalValue: T) => (value: unknown) => value is T
const is42 = literal(42)
max
<T extends number>(max: number) => (value: unknown) => value is number
const isMax255 = max(255)
maybe
<T>(predicate: Predicate<T>) => (value: unknown) => value is T | null | undefined
const isMaybeString = maybe(isString)
min
(min: number) => (value: unknown) => value is number
const isMin18 = min(18)
noneable
Aliases maybe
nullable
<T>(predicate: Predicate<T>) => (value: unknown) => value is T | null
const isNullableString = nullable(isString)
object
<T>(predicate: Predicate<T>) => (value: unknown) => value is Record<any, T>
const isEnum = object(isUint)
optional
<T>(predicate: Predicate<T>) => (value: unknown) => value is T | undefined
const isOptionalString = optional(isString)
or
<T extends Array<Predicate<any>>>(...predicates: T) => (value: unknown) => value is Static<T[number]>
const isStringOrNumber = or(isString, isNumber)
record
<T extends PropertyKey, U>(keyPredicate: Predicate<T>, valuePredicate: Predicate<U>) => (value: unknown) => value is Record<T, U>
const isDictionary = record(isString, isInt)
shape
<T extends Record<PropertyKey, Predicate<any>>>(predicates: T) => (value: unknown) => value is { [P in keyof T]: Static<T[P]> }
Note: Actual signature also considers optional members ({ name?: T }
) in its Predicate
type
const isCoordinate = shape({ x: isNumber, y: isNumber })
test
(expression: RegExp) => (value: unknown) => value is string
const isSlug = test(/^[\w-]+$/)
tuple
<T extends Array<any>>(...predicates: { [K in keyof T]: Predicate<T[K]> }) => (value: unknown) => value is T
const isPoint = tuple(isNumber, isNumber)
Predicates
isAny
(value: unknown) => value is any
Always returns true
.
isAny(value)
isArray
(value: unknown) => value is Array<unknown>
isArray(value)
isArrayLike
(value: unknown) => value is Record<number, unknown>
isArrayLike(value)
isBigInt
(value: unknown) => value is bigint
isBigInt(value)
isBoolean
(value: unknown) => value is boolean
isBoolean(value)
isDate
(value: unknown) => value is Date
isDate(value)
isDictionary
(value: unknown) => value is Record<any, string>
isDictionary(value)
isFalse
(value: unknown) => value is false
isFalse(value)
isFunction
(value: unknown) => value is Function
isFunction(value)
isInt
(value: unknown) => value is number
isInt(value)
isInt8
(value: unknown) => value is number
isInt8(value)
isInt16
(value: unknown) => value is number
isInt16(value)
isInt32
(value: unknown) => value is number
isInt32(value)
isLength
(value: unknown) => value is number
isLength(value)
isMap
(value: unknown) => value is Map<any, unknown>
isMap(value)
isNegative
(value: unknown) => value is number
isNegative(value)
isNever
(value: unknown) => value is never
Always returns false
;
isNever(value)
isNone
(value: unknown) => value is null | undefined
isNone(value)
isNull
(value: unknown) => value is null
isNull(value)
isNumber
(value: unknown) => value is number
isNumber(value)
isObject
(value: unknown) => value is object
isObject(value)
isObjectLike
(value: unknown) => value is ObjectLike
isObjectLike(value)
isPlainObject
(value: unknown) => value is {}
isPlainObject(value)
isPositive
(value: unknown) => value is number
isPositive(value)
isPrimitive
(value: unknown) => value is Primitive
isPrimitive(value)
isRegExp
(value: unknown) => value is RegExp
isRegExp(value)
isSerializable
(value: unknown) => value is Serializable
isSerializable(value)
isSerializableArray
(value: unknown) => value is Array<Serializable>
isSerializableArray(value)
isSerializableNumber
(value: unknown) => value is number
isSerializableNumber(value)
isSerializableObject
(value: unknown) => value is Record<string, Serializable>
isSerializableObject(value)
isSerializablePrimitive
(value: unknown) => value is SerializablePrimitive
isSerializablePrimitive(value)
isSet
(value: unknown) => value is Set<unknown>
isSet(value)
isSome
(value: unknown) => value is Some
isSome(value)
isString
(value: unknown) => value is string
isString(value)
isSymbol
(value: unknown) => value is symbol
isSymbol(value)
isTrue
(value: unknown) => value is true
isTrue(value)
isUint
(value: unknown) => value is number
isUint(value)
isUint8
(value: unknown) => value is number
isUint8(value)
isUint16
(value: unknown) => value is number
isUint16(value)
isUint32
(value: unknown) => value is number
isUint32(value)
isUndefined
(value: unknown) => value is undefined
isUndefined(value)
isWeakMap
(value: unknown) => value is WeakMap<any, unknown>
isWeakMap(value)
isWithLength
(value: unknown) => value is { length: number }
isWithLength(value)
Types
Intersect
Intersect<A | B>
Maybe
Maybe<T>
type MaybeString = Maybe<string>
None
None
Nullable
Nullable<T>
type NullableString = Nullable<string>
Optional
Optional<T>
type OptionalString = Optional<string>
Predicate
Predicate<T>
Primitive
Primitive
Serializable
Serializable
SerializableArray
SerializableArray
SerializableObject
SerializableObject
SerializablePrimitive
SerializablePrimitive
Some
Some
Some<T>
type Option<T extends Some, E extends Error> = T | E
type MaybeString = Optional<string>
type SomeString = Some<MaybeString>
Static
Static<Predicate<T>>
type True = Static<typeof isTrue>