Skip to content

arrayGuardMemo()

ts
function arrayGuardMemo<T>(itemGuard): Guard<T[]>

Validate arrays

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
itemGuardGuard<T>validates every item in the array

Returns

Guard<T[]>

a guard function that validates arrays


dictionaryGuardMemo()

ts
function dictionaryGuardMemo<Key, Value>(keyGard, valueGuard): Guard<Partial<Record<Key, Value>>>

Dictionaries are objects where all properties are optional and have the same type. In TypeScript, this can be represented by Partial<Record<string, ?>>.

Type Parameters

Type Parameter
Key extends string
Value

Parameters

ParameterTypeDescription
keyGardGuard<Key>validates every key
valueGuardGuard<Value>validates every value

Returns

Guard<Partial<Record<Key, Value>>>

a guard for a dictionary, of type Partial<Record<K, V>>

Examples

Validate a word dictionary:

ts
const isDictionary = dictionaryGuard(isString, isString)
isDictionary({ hello: 'world' }) // -> true

You can limit the set of keys; for example, to only allow lowercase strings:

ts
const isLowerCase = (data: unknown): data is Lowercase<string> =>
  typeof data === 'string' && data === data.toLowerCase()
const isDictionary = dictionaryGuard(isLowerCase, isString)
isDictionary({ hello: 'world' }) // -> true
isDictionary({ Hello: 'world' }) // -> false

nonEmptyArrayGuardMemo()

ts
function nonEmptyArrayGuardMemo<T>(itemGuard): Guard<[T, ...T[]]>

Validate non-empty arrays

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
itemGuardGuard<T>validates every item in the array

Returns

Guard<[T, ...T[]]>

a guard function that validates non-empty arrays


objectGuardCompiledMemo()

ts
function objectGuardCompiledMemo<T>(schema): Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>

Same as objectGuard, but performs just-in-time (JIT) compilation with the Function constructor, which greatly increases the execution speed of the validation. However, the JIT compilation is slow and gets executed at the time when the validation function is constructed. When using this function at the module level, it is recommended to wrap it in lazy to defer the JIT compilation to when the validation function is called for the first time. This function will be blocked in environments where the Function constructor is blocked; for example, when the Content-Security-Policy policy is set without the 'unsafe-eval' directive.

Example

Defer the JIT compilation to when the validation function is called for the first time.

ts
const isUser = lazy(() => objectGuardCompiled({
 id: isNumber,
 name: isString,
})

See

objectGuard for a non-just-in-time compiled version of this function.

Type Parameters

Type Parameter
T extends Record<string, unknown>

Parameters

ParameterTypeDescription
schema{ [K in string | number | symbol]-?: Object extends Pick<T, K> ? OptionalGuard<T[K]> : Guard<T[K]> }maps keys to validation functions.

Returns

Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>


objectGuardMemo()

ts
function objectGuardMemo<T>(schema): Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>

Objects have a fixed set of properties of different types.

See

objectGuardCompiled for just-in-time compiled version of this function.

Type Parameters

Type Parameter
T extends Record<string, unknown>

Parameters

ParameterTypeDescription
schema{ [K in string | number | symbol]-?: Object extends Pick<T, K> ? OptionalGuard<T[K]> : Guard<T[K]> }maps keys to validation functions.

Returns

Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>

Examples

ts
Object with both required and optional properties:
const isUser = objectGuard({
  id: isNumber,
  uid: isString,
  active: isBoolean,
  email: optional(isString),
})

Note that optional properties will be inferred as required properties that can be assigned undefined. See Infer > limitations for in-depth information.

Annotate explicitly:

ts
type User = {
  id: number
  name: string
}

const isUser = object<User>({
  id: isNumber,
  name: isString,
})

tupleGuardMemo()

ts
function tupleGuardMemo<T>(guards): Guard<T>

Type Parameters

Type Parameter
T extends readonly unknown[]

Parameters

ParameterTypeDescription
guards[...{ [K in string | number | symbol]: Guard<T[K<K>]> }[]]an array of guards. Each guard validates the corresponding element in the data tuple.

Returns

Guard<T>


unionGuardMemo()

ts
function unionGuardMemo<T>(...guards): (data) => data is T[number]

Executes guards in order and returns true if any guard matches. The result type is a union.

Type Parameters

Type Parameter
T extends readonly unknown[]

Parameters

ParameterTypeDescription
...guards{ [K in string | number | symbol]: Guard<T[K<K>]> }any of these guard functions must match the data.

Returns

Function

a guard function that validates unions

Parameters

ParameterTypeDescription
dataunknowndata to be validated

Returns

data is T[number]

true if the data is in the specified union

Examples

Commonly used in discriminated unions:

ts
const isResult = oneOfGuard([
 objectGuard({
   tag: equalsGuard('success')
 }),
 objectGuard({
   tag: equalsGuard('error')
  }),
])

When explicitly annotating oneOfGuard, provide a tuple of the union members as type argument:

ts
const isId = oneOfGuard<[string, number]>(isString, isNumber)

Due to a limitation of TypeScript, it is not possible to write unionGuard<string | number>() or equalsGuard<'red' | 'green' | 'blue'>(). Therefore, it is generally recommended to omit the type arguments for union types and let TypeScript infer them.