• Union is a dynamic-size type. Serializing a union has two steps:

    • Serialize an item type id in bytes as a 32 bit unsigned integer in little-endian. The item type id is the index of the inner items, and it's starting at 0.
    • Serialize the inner item.

    Type Parameters

    • T extends Record<string, CodecLike<any, any>>

    Parameters

    • codecLayout: T

      the union item record

    • Optionalfields: Record<keyof T, undefined | null | number>

      the custom item type id record

    Returns Codec<UnionEncodable<T>, UnionDecoded<T>>

    // without custom id
    union({ cafe: Uint8, bee: Uint8 })
    // with custom id
    union({ cafe: Uint8, bee: Uint8 }, { cafe: 0xcafe, bee: 0xbee })