DependentStructDecoderBuilder

type DependentStructDecoderBuilder<TFields, TIsFixedSize> = object;

A fluent builder that accumulates field decoders for a struct whose later fields may depend on the values of earlier ones.

Each call to `field` returns a new builder whose accumulated field type is widened by the newly added field. Call `build` to obtain the final Decoder once every field has been declared.

The builder tracks at the type level whether the struct decoded so far has a fixed size. Adding a FixedSizeDecoder preserves that property, while adding a VariableSizeDecoder or a field factory drops the builder to variable size and it stays variable thereafter.

Instances of this type are immutable. Calling field does not mutate the receiver; it returns a new builder.

See

createDependentStructDecoder

Type Parameters

Type ParameterDescription
TFields extends Record<string, unknown>The shape of the struct that has been accumulated so far.
TIsFixedSize extends booleantrue while every field added so far is a FixedSizeDecoder, false once any variable size decoder or factory has been added.

Methods

build()

build(): TIsFixedSize extends true ? FixedSizeDecoder<DrainOuterGeneric<TFields>> : VariableSizeDecoder<DrainOuterGeneric<TFields>>;

Finalizes the builder and returns a Decoder that decodes each declared field in turn, in the order they were added.

Returns a FixedSizeDecoder when every field has been added with a fixed size decoder, and a VariableSizeDecoder otherwise.

Returns

TIsFixedSize extends true ? FixedSizeDecoder<DrainOuterGeneric<TFields>> : VariableSizeDecoder<DrainOuterGeneric<TFields>>


field()

Call Signature

field<TName, TValue>(name, decoder): DependentStructDecoderBuilder<DrainOuterGeneric<TFields & { [K in string]: TValue }>, false>;

Adds a field decoded by a VariableSizeDecoder. Drops the builder to variable size; subsequent field calls cannot raise it back to fixed size.

Adding a field that has already been declared on this builder is a compile time error.

Type Parameters
Type Parameter
TName extends string
TValue
Parameters
ParameterType
nameTName extends keyof TFields ? never : TName
decoderVariableSizeDecoder<TValue>
Returns

DependentStructDecoderBuilder<DrainOuterGeneric<TFields & { [K in string]: TValue }>, false>

Call Signature

field<TName, TValue>(name, factory): DependentStructDecoderBuilder<DrainOuterGeneric<TFields & { [K in string]: TValue }>, false>;

Adds a field whose decoder is built from a frozen snapshot of the fields that precede it. Drops the builder to variable size since the byte length of the produced decoder cannot be known at type time.

Adding a field that has already been declared on this builder is a compile time error.

Type Parameters
Type Parameter
TName extends string
TValue
Parameters
ParameterType
nameTName extends keyof TFields ? never : TName
factoryDependentStructDecoderFieldFactory<TFields, TValue>
Returns

DependentStructDecoderBuilder<DrainOuterGeneric<TFields & { [K in string]: TValue }>, false>

Call Signature

field<TName, TValue>(name, decoder): DependentStructDecoderBuilder<DrainOuterGeneric<TFields & { [K in string]: TValue }>, TIsFixedSize>;

Adds a field decoded by a FixedSizeDecoder. Preserves the fixed size property of the builder.

Adding a field that has already been declared on this builder is a compile time error.

Type Parameters
Type Parameter
TName extends string
TValue
Parameters
ParameterType
nameTName extends keyof TFields ? never : TName
decoderFixedSizeDecoder<TValue>
Returns

DependentStructDecoderBuilder<DrainOuterGeneric<TFields & { [K in string]: TValue }>, TIsFixedSize>

On this page