Class ObjectDecoder<T>

A specific decoder for objects having the shape of T. Has additional methods for specifying the decoding rules of it's fields.

Type Parameters

  • T

    the type of the object this decoder can decode

Hierarchy (view full)

Properties

fields: {
    [key: Key]: [Decoder<any>, Key];
} = {}

Type declaration

Accessors

  • get name(): string
  • The name of this decoder (useful for error reporting).

    Returns string

Methods

  • Takes a decoder of U and produces a decoder of T & U. Returns a decoder that can decode all fields of T and U into a single object.

    Type Parameters

    • U

    Parameters

    Returns ObjectDecoder<T & U>

    an object decoder of T & U

  • Parameters

    • arg: unknown

    Returns boolean

  • An alias for transform.

    Type Parameters

    • U

    Parameters

    • tranformer: ((decoded) => U)

      transformer function

        • (decoded): U
        • Parameters

          • decoded: T

          Returns U

    Returns Decoder<U>

    a decoder of the transformed value

  • Takes a decoder of U and produces a decoder of the union of T | U.

    Useful for composing decoders that can decode many types.

    Example:

    asString.or(asNumber) // produces a decoder for string | number
    asString.or(asNumber).or(asBoolean)) // produces a decoder for string | number | boolean

    Type Parameters

    • U

    Parameters

    • other: Decoder<U>

      decoder for next type U

    Returns Decoder<T | U>

    A decoder for decoding T | U

  • Similar to decode, but throws an error if decoding fails.

    Parameters

    • arg: unknown

      value to decode

    Returns T

    decoded value

  • Tests if a value can be decoded by this decoder.

    Parameters

    • arg: unknown

      value to test

    Returns boolean

  • Makes it possible to apply some transformation on a decoded value.

    const asInt = asNumber.transform(num => parseInt(num))
    asInt.parse(117.565) // produces 117

    Type Parameters

    • U

    Parameters

    • transformer: ((decoded) => U)

      transformer function

        • (decoded): U
        • Parameters

          • decoded: T

          Returns U

    Returns Decoder<U>

    a decoder of the transformed value

  • Applies an operation that can also fail to this decoder.

    const asInt = asNumber.try(num => {
    if (Number.isInteger(number)) return success(num)
    else return failure("expected integer but got " + num)
    })
    asInt.parse(117.565) // throws error - $root: expected integer but got 117.565

    Type Parameters

    • U

    Parameters

    Returns Decoder<U>

    a decoder

  • Same as withField but allows changing the name of the field in the decoded value.

    Type Parameters

    Parameters

    • fieldName: Key

      field name

    • fieldDecoder: Decoder<V>

      decoder for field

    • fieldAlias: K

      alias for the field name

    Returns ObjectDecoder<Merge<T, Pair<K, V>>>

    a new object decoder with the added field

Generated using TypeDoc