Skip to content

equalsGuard()

ts
function equalsGuard<T>(constant): Guard<T>

Compares the input against a list of primitive values with the strict equality operator (===). The inferred type of the guard is that of a literal type; for example, equalsGuard('red') returns a Guard<'red'>.

Type Parameters

Type Parameter
T extends Primitive

Parameters

ParameterTypeDescription
constantTcompared against data with the === operator.

Returns

Guard<T>

Examples

ts
const isRed = equalsGuard('red')
isRed('red') // -> true
isRed('blue') // -> false

const isOne = equalsGuard(1)
isOne(1) // -> true
isOne(2) // -> false

Commonly used in discriminated unions:

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