From 9c50dcb4c0498bb11b54cdba47c9c425443b94a0 Mon Sep 17 00:00:00 2001 From: Joost Loohuis Date: Thu, 9 Apr 2026 17:25:05 +0200 Subject: [PATCH] Rewrite type declarations --- index.d.ts | 256 +++++++++++++++++++++++++++++------------------------ 1 file changed, 142 insertions(+), 114 deletions(-) diff --git a/index.d.ts b/index.d.ts index 391c325..e9334c7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,131 +1,158 @@ -type ToBuffer = (buffer: Buffer, value: unknown, index: number) => Buffer; -type FromBuffer = (buffer: Buffer, index: number, returnLength: boolean) => unknown; - -declare module "@athombv/data-types" { - interface DataTypeInterface { +declare module "@athombv/data-types" { + class DataType { id: number; shortName: string; length: number; - toBuffer: ToBuffer; - fromBuffer: FromBuffer; - args: unknown[]; - defaultValue: unknown; - - isAnalog: () => boolean; - inspect: () => string; - } - - interface DataTypeConstructor { - new ( - id: number, - shortName: string, - length: number, - toBuf: ToBuffer, - fromBuf: FromBuffer, - ...args: unknown[] - ): DataTypeInterface; - } - - interface DataTypeFunctionConstructor { - (...args: unknown[]): DataTypeConstructor; + toBuffer: (buffer: Buffer, value: Value, index?: number) => number; + fromBuffer: (buffer: Buffer, index?: number) => Value; + args: Array; + defaultValue: Value; + + get isAnalog(): boolean; + inspect(): string; } - - type DataTypeItem = - | DataTypeConstructor - | DataTypeFunctionConstructor - | { [name: string]: DataTypeItem }; // This OR is needed for Structs with a second level of DataTypes - - type GenericMap = { - [K in keyof T]: DataTypeItem; + + const DataTypes: { + noData: DataType, + + data8 : DataType, + data16: DataType, + data24: DataType, + data32: DataType, + data40: DataType, + data48: DataType, + data56: DataType, + + bool: DataType, + + map8 : (flags: Array) => DataType>, + map16: (flags: Array) => DataType>, + map24: (flags: Array) => DataType>, + map32: (flags: Array) => DataType>, + map40: (flags: Array) => DataType>, + map48: (flags: Array) => DataType>, + map56: (flags: Array) => DataType>, + map64: (flags: Array) => DataType>, + + uint8 : DataType, + uint16: DataType, + uint24: DataType, + uint32: DataType, + uint40: DataType, + uint48: DataType, + // uint56: DataType, + // uint64: DataType, + + int8 : DataType, + int16: DataType, + int24: DataType, + int32: DataType, + int40: DataType, + int48: DataType, + // int56: DataType, + // int64: DataType, + + enum8 : (flags: Record) => DataType, + enum16: (flags: Record) => DataType, + enum32: (flags: Record) => DataType, + + // semi: DataType, + single: DataType, + double: DataType, + + octstr: DataType, + string: DataType, + // octstr16: DataType, + // string16: DataType, + + // array + // struct + // set + // bag + + // ToD + // date + // UTC + + // clusterId + // attribId + + // bacOID + EUI48 : DataType, + EUI64 : DataType, + key128: DataType, + + //* Internal Types *// + map4 : (flags: Array) => DataType>, + uint4: DataType, + enum4: (flags: Record) => DataType, + + buffer : DataType, + buffer8 : DataType, + buffer16: DataType, + + Array0: (type: Type) => DataType>, + Array8: (type: Type) => DataType>, + FixedString: (length: number) => DataType, }; - - interface StructTypeInterface { - toJSON: () => T; - toBuffer: (buffer?: Buffer, index?: number) => Buffer; + + class BitMap { + _buffer: Buffer; + _fields: Array; + setBit(index: number, value: boolean): void; + getBit(index: number): boolean; + clearBit(index: number): void; + setBits(bits: number | Array): void; + getBits(): Array; + get length(): number; + static fromBuffer(buffer: Buffer, index: number, length: number, flags: Array): BitMap; + static toBuffer(buffer: Buffer, index: number, length: number, flags: Array, value: number): number; + static toBuffer(buffer: Buffer, index: number, length: number, flags: Array | undefined, value: BitMap): number; + toArray(): Array; + toBuffer(buffer: Buffer, index: number): Buffer; + copy(): BitMap; + toJSON(): object; + inspect(): string; } - - export interface StructInstance { - fromBuffer: (buffer: Buffer) => StructType & StructTypeInterface; - toBuffer: (buffer: Buffer, object: StructType, index?: number) => Buffer; - fields: GenericMap; - name: string; - length: number; - fromJSON: (object: StructType) => StructType & StructTypeInterface; - fromArgs: (...args: unknown[]) => StructType & StructTypeInterface; + + interface StaticStruct>> { + get fields(): Defs; + get name(): string; + get length(): number; + fromJSON(props: any): StructInstance; + fromArgs(...args: Array): StructInstance; + fromBuffer(buffer: Buffer, index?: number, returnLength?: false): StructInstance; + fromBuffer(buffer: Buffer, index?: number, returnLength?: true): { + result: StructInstance, + length: number, + } + toBuffer(buffer: Buffer, value: StructInstance, index?: number): number; } - export const DataTypes: { - noData: DataTypeConstructor; - data8: DataTypeConstructor; - data16: DataTypeConstructor; - data24: DataTypeConstructor; - data32: DataTypeConstructor; - data40: DataTypeConstructor; - data48: DataTypeConstructor; - data56: DataTypeConstructor; - data64: DataTypeConstructor; - bool: DataTypeConstructor; - map8: DataTypeFunctionConstructor; - map16: DataTypeFunctionConstructor; - map24: DataTypeFunctionConstructor; - map32: DataTypeFunctionConstructor; - map40: DataTypeFunctionConstructor; - map48: DataTypeFunctionConstructor; - map56: DataTypeFunctionConstructor; - map64: DataTypeFunctionConstructor; - uint8: DataTypeConstructor; - uint16: DataTypeConstructor; - uint24: DataTypeConstructor; - uint32: DataTypeConstructor; - uint40: DataTypeConstructor; - uint48: DataTypeConstructor; - int8: DataTypeConstructor; - int16: DataTypeConstructor; - int24: DataTypeConstructor; - int32: DataTypeConstructor; - int40: DataTypeConstructor; - int48: DataTypeConstructor; - enum8: DataTypeFunctionConstructor; - enum16: DataTypeFunctionConstructor; - enum32: DataTypeFunctionConstructor; - single: DataTypeConstructor; - double: DataTypeConstructor; - octstr: DataTypeConstructor; - string: DataTypeConstructor; - EUI48: DataTypeConstructor; - EUI64: DataTypeConstructor; - key128: DataTypeConstructor; - uint4: DataTypeConstructor; - enum4: DataTypeFunctionConstructor; - map4: DataTypeFunctionConstructor; - buffer: DataTypeConstructor; - buffer8: DataTypeConstructor; - buffer16: DataTypeConstructor; - Array0: DataTypeFunctionConstructor; - Array8: DataTypeFunctionConstructor; - FixedString: DataTypeFunctionConstructor; - }; - export const DataType: DataTypeInterface; - export function Struct( - name: string, - objectDefinition: GenericMap - ): StructInstance; + + type StructInstance>> = StructProperties & { + toJSON: () => StructProperties; + toBuffer: (buffer: Buffer, index?: number) => Buffer; + } + + function Struct>> (name: string, defs: Defs, opts?: {encodeMissingFieldsBehavior?: 'default' | 'skip'}): StaticStruct; +} + +type StructProperties>> = { + [Property in keyof Defs]: Defs[Property] extends import('@athombv/data-types').DataType ? Type : never } + /* How to use @athombv/data-types in TypeScript: // Create a type that represents the Struct data -type ZdoEndDeviceAnnounceIndication = { - srcAddr: number; - IEEEAddr: string; +const ZdoEndDeviceAnnounceIndication = { + srcAddr: DataTypes.uint16, + IEEEAddr: DataTypes.EUI64, }; // Create a Struct instance with generic type ZdoEndDeviceAnnounceIndication -const ZdoEndDeviceAnnounceIndicationStruct = - Struct("ZdoEndDeviceAnnounceIndication", { - srcAddr: DataTypes.uint16, - IEEEAddr: DataTypes.EUI64, - }); +const ZdoEndDeviceAnnounceIndicationStruct = Struct("ZdoEndDeviceAnnounceIndication", ZdoEndDeviceAnnounceIndication); // Create ZdoEndDeviceAnnounceIndication object const ZdoEndDeviceAnnounceObject = ZdoEndDeviceAnnounceIndicationStruct.fromBuffer( @@ -135,9 +162,10 @@ const ZdoEndDeviceAnnounceObject = ZdoEndDeviceAnnounceIndicationStruct.fromBuff ZdoEndDeviceAnnounceObject.srcAddr.trim(); // This errors, srcAddr is not a string // Create Buffer instance from ZdoEndDeviceAnnounceObject -const ZdoEndDeviceAnnounceBuffer = ZdoEndDeviceAnnounceIndicationStruct.toBuffer({ srcAddr: 1, IEEAddr: 'abc' }); // This errors due to typo in IEEEAddr name +const ZdoEndDeviceAnnounceBuffer = Buffer.alloc(8); +ZdoEndDeviceAnnounceIndicationStruct.toBuffer(ZdoEndDeviceAnnounceBuffer, { srcAddr: 1, IEEAddr: 'abc' }); // This errors due to typo in IEEEAddr name Known limitations: - Structs in Structs are considered a no-go by these definitions. -- DataTypes have no related JS type, so a few unknowns are used. +- Struct.fromArgs cannot be typed. */