decoders

Constants

Decoders for constant and fixed values.

constant

function constant( value: T ): Decoder<T> (source)

Accepts only the given constant value.

Try it
constant('hello').verify(input)
InputResult
"hello"
"this breaks" ^^^^^^^^^^^^^ Must be 'hello'
false ^^^^^ Must be 'hello'
undefined ^^^^^^^^^ Must be 'hello'

null_

null_: Decoder<null> (source)

Accepts and returns only the literal null value. iNamed null_ because null is a reserved keyword in TypeScript.

Try it
null_.verify(input)
InputResult
null
false ^^^^^ Must be null
undefined ^^^^^^^^^ Must be null
"hello world" ^^^^^^^^^^^^^ Must be null

undefined_

undefined_: Decoder<undefined> (source)

Accepts and returns only the literal undefined value. iNamed undefined_ because undefined is a reserved keyword in TypeScript.

Try it
undefined_.verify(input)
InputResult
undefined
null ^^^^ Must be undefined
false ^^^^^ Must be undefined
"hello world" ^^^^^^^^^^^^^ Must be undefined

enum_

function enum_( enum: MyEnum ): Decoder<MyEnum> (source)

Accepts and returns an enum value. iNamed enum_ because enum is a reserved keyword in TypeScript.

It works with numeric enums:

Try it
enum Fruit {  Apple,  Banana,  Cherry,}enum_(Fruit).verify(input)
InputResult
0
1
0
1
4 ^ Must be one of 0, 1, 2
"Apple" ^^^^^^^ Must be one of 0, 1, 2

As well as with string enums:

Try it
enum Fruit {  Apple = 'a',  Banana = 'b',  Cherry = 'c',}enum_(Fruit).verify(input)
InputResult
"a"
"b"
"a"
"b"
"Apple" ^^^^^^^ Must be one of 'a', 'b', 'c'
0 ^ Must be one of 'a', 'b', 'c'

On this page