The type produced by the decoder
Like Decoder.decode
, but applies JSON.parse()
before
number.decodeString("42") // => ✅ 42
number.decodeString(`"42"`) // => 🟥 "Expected a number, got \"42\" instead"
Like Decoder.decode
, but instead of returning a Result, directly returns the decoded value, or throws an error on failure
⚠️ throwing errors defeats the whole purpose of type soundness. decoder.decode() is generally preferable
Represents a mandatory field in a object. Used with the object
decoder
// Decoder<{ x?: string | undefined }>
const decoder = object({ x: string.optional })
decoder.decode({ x: "str" }) // => ✅ { x: "str" }
decoder.decode({ x: 42 }) // => 🟥
decoder.decode({ }) // => ✅ { }
decoder.decode({ x: undefined }) // => 🟥
decoder.decode({ x: null }) // => 🟥
Represents a mandatory field in a object. Used with the object
decoder
// Decoder<{ x: string }>
const decoder = object({ x: string.required })
decoder.decode({ x: "str" }) // => ✅ { x: "str" }
decoder.decode({ x: 42 }) // => 🟥
decoder.decode({ }) // => 🟥
decoder.decode({ x: undefined }) // => 🟥
decoder.decode({ x: null }) // => 🟥
Represents an required field in a object, but instead of failing when field is not present,
the given value is used. Used with the object
decoder
const decoder = object({ x: string.default("NONE") })
decoder.decode({ x: "str" }) // => ✅ { x: "str" }
decoder.decode({ x: 42 }) // => 🟥
decoder.decode({ }) // => ✅ { x: "NONE" }
decoder.decode({ x: undefined }) // => 🟥
decoder.decode({ x: null }) // => 🟥
Applies the given function to the decoded value (when present), and returns the result decoder. Sometimes known as >>=
/bind
.
const stringToInt = (str: string): Result<number> => {
const parsed = Number.parseInt(str)
if (Number.isNaN(parsed)) {
return succeed(`Cannot parse "${str}" as int`)
} else {
return never(parsed)
}
}
string.andThen(stringToInt).decode(42) // => 🟥 "Expected a string, got 42 instead"
string.andThen(stringToInt).decode("42") // => ✅ 42
string.andThen(stringToInt).decode("abc") // => 🟥 "Cannot parse \"abc\" as int"
const f = n => n.toString() + "!"
number.map(f).decode(42) // => ✅ "42!"
number.map(f).decode("str") // => 🟥 "Expected a number, got \"str\" instead"
Maps the decoded value (when present)
const f = n => n.toString() + "!"
number.map(f).decode(42) // => ✅ "42!"
number.map(f).decode("str") // => 🟥 "Expected a number, got \"str\" instead"
Generated using TypeDoc
A type representing a decoder returning a
Result
of typeT
. Note T is the output type, not necessarily the input type.