• Combine multiple decoders where any of them can match for the resulting decoder to succeed.

    Type Parameters

    • T

    Parameters

    • decoders: Decoder<T>[]

      A list of decoders that will be executed

    Returns Decoder<T>

    Example

    For this example we assume that we have an API endpoint that returns either a numeric string or a number. Something like this:

    [
    { id: 1 },
    { id: '2' },
    ]

    which can be decoded to a flat numeric array like this:

    const string_to_number_decoder = Decode.andThen(str => {
    const v = parseInt(str, 10);

    if (typeof v === 'number' && !isNaN(v)) {
    return Decode.succeed(v);
    } else {
    return Decode.fail(expected('a number string', str));
    }
    }, Decode.string);

    const decoder = Decode.oneOf([
    Decode.number,
    string_to_number_decoder,
    ]);

    // This gives us an array like [1, 2], where both values are actual
    // numbers
    decode(Decode.many(Decode.field('id', decoder)), [
    { id: 1 },
    { id: '2' },
    ]);

Generated using TypeDoc