getLiteralUnionCodec
Call Signature
Returns a codec for encoding and decoding literal unions.
A literal union codec serializes and deserializes values
from a predefined set of literals, using a numerical index
to represent each value in the variants array.
This allows efficient storage and retrieval of common predefined values such as enum-like structures in TypeScript.
Type Parameters
| Type Parameter | Description |
|---|---|
TVariants extends readonly Variant[] | A tuple of allowed literal values. |
Parameters
| Parameter | Type | Description |
|---|---|---|
variants | TVariants | The possible literal values for the union. |
Returns
FixedSizeCodec<GetTypeFromVariants<TVariants>, GetTypeFromVariants<TVariants>, 1>
A FixedSizeCodec or VariableSizeCodec for encoding and decoding literal unions.
Examples
Encoding and decoding a union of string literals.
Encoding and decoding a union of number literals.
Using a custom discriminator size with different variant types.
Remarks
Separate getLiteralUnionEncoder and getLiteralUnionDecoder functions are available.
See
Call Signature
Returns a codec for encoding and decoding literal unions.
A literal union codec serializes and deserializes values
from a predefined set of literals, using a numerical index
to represent each value in the variants array.
This allows efficient storage and retrieval of common predefined values such as enum-like structures in TypeScript.
Type Parameters
| Type Parameter | Description |
|---|---|
TVariants extends readonly Variant[] | A tuple of allowed literal values. |
TSize extends number | - |
Parameters
| Parameter | Type | Description |
|---|---|---|
variants | TVariants | The possible literal values for the union. |
config | LiteralUnionCodecConfig<NumberCodec> & object | Configuration options for encoding and decoding the literal union. |
Returns
FixedSizeCodec<GetTypeFromVariants<TVariants>, GetTypeFromVariants<TVariants>, TSize>
A FixedSizeCodec or VariableSizeCodec for encoding and decoding literal unions.
Examples
Encoding and decoding a union of string literals.
Encoding and decoding a union of number literals.
Using a custom discriminator size with different variant types.
Remarks
Separate getLiteralUnionEncoder and getLiteralUnionDecoder functions are available.
See
Call Signature
Returns a codec for encoding and decoding literal unions.
A literal union codec serializes and deserializes values
from a predefined set of literals, using a numerical index
to represent each value in the variants array.
This allows efficient storage and retrieval of common predefined values such as enum-like structures in TypeScript.
Type Parameters
| Type Parameter | Description |
|---|---|
TVariants extends readonly Variant[] | A tuple of allowed literal values. |
Parameters
| Parameter | Type | Description |
|---|---|---|
variants | TVariants | The possible literal values for the union. |
config? | LiteralUnionCodecConfig<NumberCodec> | Configuration options for encoding and decoding the literal union. |
Returns
VariableSizeCodec<GetTypeFromVariants<TVariants>>
A FixedSizeCodec or VariableSizeCodec for encoding and decoding literal unions.
Examples
Encoding and decoding a union of string literals.
Encoding and decoding a union of number literals.
Using a custom discriminator size with different variant types.
Remarks
Separate getLiteralUnionEncoder and getLiteralUnionDecoder functions are available.