Constants
Decoders for constant and fixed values.
constant
function constant( value: T ): Decoder<T> (source)
Accepts only the given constant value.
Try it
| Input | Result |
|---|---|
| "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. Named null_ because null is a
reserved keyword in TypeScript.
Try it
| Input | Result |
|---|---|
| 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. Named undefined_ because
undefined is a reserved keyword in TypeScript.
Try it
| Input | Result |
|---|---|
| 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. Named enum_ because enum is a reserved
keyword in TypeScript.
It works with numeric enums:
Try it
| Input | Result |
|---|---|
| 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
| Input | Result |
|---|---|
| "a" | |
| "b" | |
| "a" | |
| "b" | |
| "Apple" ^^^^^^^ Must be one of 'a', 'b', 'c' | |
| 0 ^ Must be one of 'a', 'b', 'c' |