getUnionEncoder
Returns an encoder for union types.
This encoder serializes values by selecting the correct variant encoder
based on the getIndexFromValue function.
Unlike other codecs, this encoder does not store the variant index. It is the user's responsibility to manage discriminators separately.
For more details, see getUnionCodec.
Type Parameters
| Type Parameter | Description |
|---|---|
TVariants extends readonly Encoder<any>[] | An array of encoders, each corresponding to a union variant. |
Parameters
| Parameter | Type | Description |
|---|---|---|
variants | TVariants | The encoders for each variant of the union. |
getIndexFromValue | (value) => number | A function that determines the variant index from the provided value. |
Returns
UnionEncoder<TVariants>
An Encoder for encoding union values.
Example
Encoding a union of numbers and booleans.