getLiteralUnionDecoder

Call Signature

function getLiteralUnionDecoder<TVariants>(
    variants,
): FixedSizeDecoder<GetTypeFromVariants<TVariants>, 1>;

Returns a decoder for literal unions.

This decoder deserializes a numerical index into a corresponding value from a predefined set of literals.

For more details, see getLiteralUnionCodec.

Type Parameters

Type ParameterDescription
TVariants extends readonly Variant[]A tuple of allowed literal values.

Parameters

ParameterTypeDescription
variantsTVariantsThe possible literal values for the union.

Returns

FixedSizeDecoder<GetTypeFromVariants<TVariants>, 1>

A FixedSizeDecoder or VariableSizeDecoder for decoding literal unions.

Example

Decoding a union of string literals.

type Size = 'small' | 'medium' | 'large';
const sizeDecoder = getLiteralUnionDecoder(['small', 'medium', 'large']);
 
sizeDecoder.decode(new Uint8Array([0x00])); // 'small'
sizeDecoder.decode(new Uint8Array([0x01])); // 'medium'
sizeDecoder.decode(new Uint8Array([0x02])); // 'large'

See

getLiteralUnionCodec

Call Signature

function getLiteralUnionDecoder<TVariants, TSize>(
    variants,
    config,
): FixedSizeDecoder<GetTypeFromVariants<TVariants>, TSize>;

Returns a decoder for literal unions.

This decoder deserializes a numerical index into a corresponding value from a predefined set of literals.

For more details, see getLiteralUnionCodec.

Type Parameters

Type ParameterDescription
TVariants extends readonly Variant[]A tuple of allowed literal values.
TSize extends number-

Parameters

ParameterTypeDescription
variantsTVariantsThe possible literal values for the union.
configLiteralUnionCodecConfig<NumberDecoder> & objectConfiguration options for decoding the literal union.

Returns

FixedSizeDecoder<GetTypeFromVariants<TVariants>, TSize>

A FixedSizeDecoder or VariableSizeDecoder for decoding literal unions.

Example

Decoding a union of string literals.

type Size = 'small' | 'medium' | 'large';
const sizeDecoder = getLiteralUnionDecoder(['small', 'medium', 'large']);
 
sizeDecoder.decode(new Uint8Array([0x00])); // 'small'
sizeDecoder.decode(new Uint8Array([0x01])); // 'medium'
sizeDecoder.decode(new Uint8Array([0x02])); // 'large'

See

getLiteralUnionCodec

Call Signature

function getLiteralUnionDecoder<TVariants>(
    variants,
    config?,
): VariableSizeDecoder<GetTypeFromVariants<TVariants>>;

Returns a decoder for literal unions.

This decoder deserializes a numerical index into a corresponding value from a predefined set of literals.

For more details, see getLiteralUnionCodec.

Type Parameters

Type ParameterDescription
TVariants extends readonly Variant[]A tuple of allowed literal values.

Parameters

ParameterTypeDescription
variantsTVariantsThe possible literal values for the union.
config?LiteralUnionCodecConfig<NumberDecoder>Configuration options for decoding the literal union.

Returns

VariableSizeDecoder<GetTypeFromVariants<TVariants>>

A FixedSizeDecoder or VariableSizeDecoder for decoding literal unions.

Example

Decoding a union of string literals.

type Size = 'small' | 'medium' | 'large';
const sizeDecoder = getLiteralUnionDecoder(['small', 'medium', 'large']);
 
sizeDecoder.decode(new Uint8Array([0x00])); // 'small'
sizeDecoder.decode(new Uint8Array([0x01])); // 'medium'
sizeDecoder.decode(new Uint8Array([0x02])); // 'large'

See

getLiteralUnionCodec

On this page