Skip to content

nullableGuard()

ts
function nullableGuard<T>(guard): (data) => data is null | T

Create a union with null. Convenient when creating nullable properties in objects. Alias for unionGuard(isNull, guard).

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
guardGuard<T>

Returns

Function

Parameters

ParameterTypeDescription
dataunknowndata to be validated

Returns

data is null | T

true if the data is in the specified union


optionalGuard()

ts
function optionalGuard<T>(guard): OptionalGuard<T>

Represent an optional property. Note that in TypeScript, optional properties may be assigned undefined or omitted entirely from the object.

Example

Wrap properties in optional to make them optional:

ts
type User = {
 id: number
 email?: string
}
const isUser = objectGuard<User>({
  id: isNumber,
  email: optionalGuard(isString),
})
isUser({ id: 123 }) // -> true
isUser({ id: 123, email: undefined }) // -> true
isUser({ id: 123, email: 'abc@test.com' }) // -> true

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
guardGuard<T>

Returns

OptionalGuard<T>


optionalNullableGuard()

ts
function optionalNullableGuard<T>(guard): OptionalGuard<null | T>

Create an optional property that also can be null. Convenient when creating optional nullable properties in objects. Alias for optional(oneOfGuard(isNull, guard)).

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
guardGuard<T>

Returns

OptionalGuard<null | T>


undefineableGuard()

ts
function undefineableGuard<T>(guard): (data) => data is undefined | T

Create a union with undefined, which is different from optional properties. Alias for unionGuard(isUndefined, guard).

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
guardGuard<T>

Returns

Function

Parameters

ParameterTypeDescription
dataunknowndata to be validated

Returns

data is undefined | T

true if the data is in the specified union