Skip to content

Documentation / @andrew_l/tl-pack / Structure / Constructor

Interface: Constructor<T> ​

Base Structure class for binary serialization with type safety

Examples ​

ts
// Direct usage (not recommended, use defineStructure instead)
class CustomStruct extends Structure<{id: number, name: string}> {
  // Custom implementation
}
ts
// Typical usage through defineStructure
const MyStruct = defineStructure({
  name: 'MyStruct',
  version: 1,
  properties: { id: { type: Number, required: true } }
});

const instance = new MyStruct({ id: 42 });
console.log(instance.value.id); // 42

Type Parameters ​

T ​

T extends Data = any

Data type extending Data interface

Constructors ​

Constructor ​

new Constructor(value): Structure<T>

Creates a new Structure instance

Parameters ​

value ​

T

The data to structure

Returns ​

Structure<T>

Example ​

ts
const data = { id: 123, name: 'Test' };
const struct = new MyStructure(data);

Properties ​

estimatedSizeBytes ​

readonly estimatedSizeBytes: number

Estimated size of bytes


extension ​

readonly extension: TLExtension

Structure extension defining encoding/decoding behavior


structures ​

readonly structures: Constructor<any>[]

Nested structures defining encoding/decoding behavior

Methods ​

fromBuffer() ​

fromBuffer(buffer, options?): T

Deserializes a structure from a binary buffer

Parameters ​

buffer ​

Uint8Array

Binary data to deserialize

options? ​

BinaryReaderOptions

Reader configuration options

Returns ​

T

Deserialized structure data

Example ​

ts
const buffer = user.toBuffer();
const restored = User.fromBuffer(buffer);
console.log(restored.id); // Original user ID