getEnumCodec
Call Signature
Returns a codec for encoding and decoding enums.
This codec serializes enums as a numerical discriminator, allowing them to be efficiently stored and reconstructed from binary data.
By default, the discriminator is derived from the positional index of the enum variant, but it can be configured to use the enum's numeric values instead.
Type Parameters
| Type Parameter | Description |
|---|---|
TEnum extends EnumLookupObject | The TypeScript enum or object mapping enum keys to values. |
Parameters
| Parameter | Type | Description |
|---|---|---|
constructor | TEnum | The constructor of the enum. |
config? | Omit<EnumCodecConfig<NumberCodec>, "size"> | Configuration options for encoding and decoding the enum. |
Returns
FixedSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>, 1>
A FixedSizeCodec or VariableSizeCodec for encoding and decoding enums.
Examples
Encoding and decoding enums using positional indexes.
Encoding and decoding enums using their numeric values.
Note that, when using values as discriminators, the enum values must be numerical. Otherwise, an error will be thrown.
Using a custom discriminator size.
Remarks
Separate getEnumEncoder and getEnumDecoder functions are available.
See
Call Signature
Returns a codec for encoding and decoding enums.
This codec serializes enums as a numerical discriminator, allowing them to be efficiently stored and reconstructed from binary data.
By default, the discriminator is derived from the positional index of the enum variant, but it can be configured to use the enum's numeric values instead.
Type Parameters
| Type Parameter | Description |
|---|---|
TEnum extends EnumLookupObject | The TypeScript enum or object mapping enum keys to values. |
TSize extends number | - |
Parameters
| Parameter | Type | Description |
|---|---|---|
constructor | TEnum | The constructor of the enum. |
config | EnumCodecConfig<NumberCodec> & object | Configuration options for encoding and decoding the enum. |
Returns
FixedSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>, TSize>
A FixedSizeCodec or VariableSizeCodec for encoding and decoding enums.
Examples
Encoding and decoding enums using positional indexes.
Encoding and decoding enums using their numeric values.
Note that, when using values as discriminators, the enum values must be numerical. Otherwise, an error will be thrown.
Using a custom discriminator size.
Remarks
Separate getEnumEncoder and getEnumDecoder functions are available.
See
Call Signature
Returns a codec for encoding and decoding enums.
This codec serializes enums as a numerical discriminator, allowing them to be efficiently stored and reconstructed from binary data.
By default, the discriminator is derived from the positional index of the enum variant, but it can be configured to use the enum's numeric values instead.
Type Parameters
| Type Parameter | Description |
|---|---|
TEnum extends EnumLookupObject | The TypeScript enum or object mapping enum keys to values. |
Parameters
| Parameter | Type | Description |
|---|---|---|
constructor | TEnum | The constructor of the enum. |
config? | EnumCodecConfig<NumberCodec> | Configuration options for encoding and decoding the enum. |
Returns
VariableSizeCodec<GetEnumFrom<TEnum>, GetEnumTo<TEnum>>
A FixedSizeCodec or VariableSizeCodec for encoding and decoding enums.
Examples
Encoding and decoding enums using positional indexes.
Encoding and decoding enums using their numeric values.
Note that, when using values as discriminators, the enum values must be numerical. Otherwise, an error will be thrown.
Using a custom discriminator size.
Remarks
Separate getEnumEncoder and getEnumDecoder functions are available.