Function asCustom

  • A way of creating custom decoders that can decoder a value into a specified type T. Useful only when utility for decoding your desired value doesn't exist.

    example:

    // a decoder for decoding a string that should be one of 3 valid
    // values: 'accepted', 'rejected', 'in-review'
    const customDecoder = asCustom((value) => {
    if (value === 'accepted' || value === 'rejected' || value === 'in-review')
    return success(value)
    return failure(`failed to decode value, expected one of 'accepted', 'rejected', 'in-review' but got ${value}`)
    }, 'status decoder')

    Type Parameters

    • T

    Parameters

    • decodeFn: ((arg) => DecodingResult<T>)

      a decode function used in decoding a value to type T

    • Optional testFn: ((arg) => boolean)

      an optional test function used in testing a value for conformity to type T

        • (arg): boolean
        • Parameters

          • arg: unknown

          Returns boolean

    • Optional name: string

      an optional name of your custom decoder

    Returns Decoder<T>

    a new Decoder

Generated using TypeDoc