Skip to content

equals()

ts
function equals<T>(constant): Parser<T>

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

Type Parameters

Type Parameter
T extends Primitive

Parameters

ParameterTypeDescription
constantTOne or more primitive values that are compared against data with the === operator.

Returns

Parser<T>

A parser function that validates the input against the provided constants.

Examples

ts
const parseInfo = equals('info')
parseInfo('info') // => ParseSuccess<'info'>
parseInfo('error') // => ParseFailure

const parseOne = equals(1)
parseOne(1) // => ParseSuccess<1>
parseOne(2) // => ParseFailure

Commonly used in discriminated unions:

ts
const parseResult = oneOf([
 object({
   tag: equals('success')
 }),
 object({
   tag: equals('error')
  }),
])