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
| Parameter | Type | Description |
|---|---|---|
constant | T | One 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) // => ParseFailureCommonly used in discriminated unions:
ts
const parseResult = oneOf([
object({
tag: equals('success')
}),
object({
tag: equals('error')
}),
])