Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Decoder<T>

A type representing a decoder returning a Result of type T. Note T is the output type, not necessarily the input type.

number; // Decoder<number>
number.map((num) => (num + 10).toString()); // Decoder<string>

Type parameters

  • T = unknown

    The type produced by the decoder

Hierarchy

Index

Decode

Readonly decode

decode: (value: unknown) => Result<T>

Type declaration

    • Run the decode through the given value (of unknown type) and produce a Result

      number.decodeString("42") // => ✅ 42
      number.decodeString(`"42"`) // => 🟥 "Expected a number, got \"42\" instead"

      Parameters

      • value: unknown

      Returns Result<T>

decodeString

  • decodeString(json: string): Result<T>
  • Like Decoder.decode, but applies JSON.parse() before

    number.decodeString("42") // => ✅ 42
    number.decodeString(`"42"`) // => 🟥 "Expected a number, got \"42\" instead"

    Parameters

    • json: string

    Returns Result<T>

decodeUnsafeThrow

  • decodeUnsafeThrow(value: unknown): T
  • 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

    Parameters

    • value: unknown

    Returns T

Object

optional

optional: OptionalField<T> = ...

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 }) // => 🟥

required

required: RequiredField<T> = ...

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 }) // => 🟥

default

  • default(value: T): RequiredField<T>
  • 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 }) // => 🟥

    Parameters

    • value: T

    Returns RequiredField<T>

Transform

andThen

  • 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"

    Type parameters

    • U

    Parameters

    Returns Decoder<U>

map

  • map<U>(f: (value: T) => U): Decoder<U>
  • 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"

    Type parameters

    • U

    Parameters

    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns Decoder<U>

Generated using TypeDoc